如何在 ASP.NET 中按类而不是 ID 选择元素? [英] How to select an element by Class instead of ID in ASP.NET?
问题描述
我在 aspx 页面上有一些分散的 <p>
元素,我使用这样的类将它们组合在一起 - <p class="instructions" runat="server">
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">
在我后面的代码中,使用 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 名称...
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# 代码隐藏/ASP.NET 中完成
I can't use JavaScript, so the selection must be done in C# codebehind/ASP.NET
推荐答案
除了将所有控件分组到一个容器控件中之外,没有简单的方法可以在 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.
在客户端,您可以使用诸如 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 中删除它 - 内容只是隐藏在那里.在服务器端隐藏控件可以防止它们的内容被发送到 HTML.
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# 中做同样的事情有点困难 - 它需要递归遍历控件树并在 Control
集合中寻找匹配的元素.这是一个足够常见的操作,效用函数很有用.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;
}
这篇关于如何在 ASP.NET 中按类而不是 ID 选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!