更改asp.net中服务器控件的ID
大家好,
我使用 find 控件从内容页面中查找母版页内无序列表的列表项,
Control home = this.Page.Master.FindControl("list").FindControl("home");
现在我必须将控件主页的 id 更改为“当前”,因为要为其应用 css... 。
Hai guys,
I used find control to find a list item of an unoreder list inside a master page from content page using this,
Control home = this.Page.Master.FindControl("list").FindControl("home");
Now i have to change the id of the control home to "current" because to apply css for it....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您知道您正在查找的控件的类型吗?既不是 Control 也不是 ListItem 公开 CssClass 属性,但是,ListItem 确实公开它是 Attributes 属性。
根据评论和其他问题:
您应该使用
System.Web.UI.HtmlControls.HtmlGenericControl
所以像这样的东西应该适合您:
如果您认为有可能已经是分配给它的类,您需要附加到您可以执行以下操作:
如果它们不是 ListItems,请将它们转换为正确的类型,然后像以前一样修改属性集合,除非该类型有 CssClass 属性。
Do you know the Type of the control you're finding? Neither Control nor ListItem expose a CssClass property, however, ListItem does expose it's Attributes property.
Update based on comment and other question:
You should be using
System.Web.UI.HtmlControls.HtmlGenericControl
So something like this should work for you:
If you think there might already be an class assigned to it that you need to append to you could do something like:
If they aren't ListItems, cast them to the correct type, and modify the attributes collection as before, unless there's a CssClass property for that type.
是的,在 asp.net 中使用 css id 是一个大问题。首先,您可以将服务器控件的 id 更改为您想要的内容,但是 ASP.NET 将根据该控件在页面控件树中的位置重新生成该 ID。
我的建议是使用控件的 cssclass 属性,并将 css id 替换为 class。
Yes, to use css id's in asp.net is a big problem. first of all you can change your server control's id to what you want but, this will be regenerated by ASP.NET depending on the position of the control in your page's control tree.
My recommendation is to use cssclass property of the controls, and to replace the css ids to class.