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

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

问题描述

我在aspx页面上有一些散布的< p> 元素,我使用类 - < p class =instructionsrunat =server>



在我的代码中,使用C#我想隐藏这些元素,像
instructions.Visible = false;



但是我意识到我只能在codebehind if我使用ID,但这将导致无效的HTML / CSS选择器,因为您不能有多个ID具有相同的ID名称...



或者有另一种方法如果不是按类分组控件?



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

解决方案

除了将单个容器控件中的所有控件分组之外,没有简单的方法来找到一组给定ASP中的属性的控件。 NET服务器端代码。



在客户端,您可以使用 jQuery 找到这些元素并隐藏它们:

  $(。 

我可能会在页面完全加载时做出响应:

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

在Javascript中隐藏元素的一个缺点是,如果有足够的数据,可能需要一秒钟,闪烁。另一个区别是,隐藏内容客户端不会从DOM中删除它 - 内容刚刚隐藏。隐藏控件服务器端阻止其内容甚至被发送到HTML。



在C#中做同样的事情有点困难 - 它需要递归遍历控制树和在匹配的 Control 集合中查找元素。这是一个足够普遍的操作,效用函数是有用的。 C#迭代器语法(yield return)有助于使得这个干净:

  //实用方法递归地找到匹配谓词的控制
IEnumerable& FindRecursive(Control c,Func< Control,bool>谓词)
{
if(谓词(c))
yield return c;

foreach(c.Controls中的var子项)
{
if(谓词(c))
yield return c;
}

foreach(c.Controls中的var child)
foreach(FindRecursive(c,predicate)中的var匹配)
yield return match;
}

//使用实用程序方法来查找匹配的控件...
FindRecursive(Page,c =>(c是WebControl)&&
((WebControl)c).CssClass ==instructions);

现在隐藏控件相对容易:

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


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">

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

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?

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

解决方案

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.

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(); 
});

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.

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;
}

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

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