如何选择按类而不是ID在ASP.NET中的元素? [英] How to select an element by Class instead of ID in ASP.NET?

查看:113
本文介绍了如何选择按类而不是ID在ASP.NET中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些零星的< P> aspx页面上的元素,我现在用的一类像组合在一起,让 - < p类=指令=服务器>

I have a few scattered <p> elements on the aspx page which I am grouping together using a class like so - <p class="instructions" runat="server">

在我的code的后面,使用C#我想隐藏这些元素,使用类似
instructions.Visible = FALSE;

In my code behind, using C# I want to hide these elements, using something like instructions.Visible = false;

不过,我知道如果我用的ID,但是这会导致无效的HTML / CSS选择器,因为你不能有多个ID的使用相同的ID名字,我只能这样做在codebehind ...

However I realize I can only do this in codebehind if I use ID but this will result in invalid HTML/CSS Selector since you can't have multiple ID's with the same ID name...

另外有另一种方式来分组控制,如果没有课吗?

Alternatively is there another way to group the controls if not by class?

编辑:我不能使用JavaScript,所以选择必须在C#codebehind / ASP.NET来完成

I can't use JavaScript, so the selection must be done in C# codebehind/ASP.NET

推荐答案

除了把所有的控件在一个容器控件,还有就是找给予一定财产ASP.NET服务器 - 一组控件没有简单的方法侧面code。

Aside from grouping all of the controls in a single container control, there is no easy way to find a group of controls given some property in ASP.NET server-side code.

在客户端,你可以使用类似 jQuery的找到这些元素并隐藏他们:

On the client side, you could use something like jQuery to find these elements and hide them:

$(".instructions").hide();

我可能会做响应当页面完全加载:

I would probably do this in response when the page is fully loaded:

$(document).ready(function() { 
   $(".instructions").hide(); 
});

一个缺点在Javascript中隐藏的元素是,如果有足够的数据可能需要一秒钟,导致内容闪烁。另一个区别是,隐藏内容客户端的的从DOM取出 - 的含量存在只是隐藏。隐藏控制服务器端$ P $甚至从发射到HTML pvents他们的内容。

One downside to hiding elements in Javascript is that if there's enough data it may take a second and cause content to flicker. Another difference is that hiding content client-side does not remove it from the DOM - the content is there just hidden. Hiding controls server-side prevents their content from even being emitted to the HTML.

在做C#一样的东西是有点困难 - 它需要递归遍历控件树,寻找匹配的控制收集要素。这是一个常见足够的操作,一个实用的功能是非常有用的。 C#语法迭代器(收益率回报)是使这种清洁有所帮助:

Doing the same thing in C# is a bit harder - it requires recursively traversing the control tree and looking for elements in the Control collection that match. This is a common enough operation that a utility function is useful. C# iterator syntax (yield return) is helpful in making this clean:

// utility method to recursively find controls matching a predicate
IEnumerable<Control> FindRecursive( Control c, Func<Control,bool> predicate )
{
    if( predicate( c ) )
        yield return c;

    foreach( var child in c.Controls )
    {
        if( predicate( c ) )
            yield return c;
    }

    foreach( var child in c.Controls )
        foreach( var match in FindRecursive( c, predicate ) )
           yield return match;
}

// use the utility method to find matching controls...
FindRecursive( Page, c => (c is WebControl) && 
                          ((WebControl)c).CssClass == "instructions" );

隐藏控件现在是比较容易的:

Hiding the controls now is relatively easy:

foreach( WebControl c in FindRecursive( Page, c => (c is WebControl) && 
                           ((WebControl)c).CssClass == "instructions" ) )
{
    c.Visible = false;
}

这篇关于如何选择按类而不是ID在ASP.NET中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆