更改asp.net中服务器控件的ID

发布于 2024-08-11 23:59:41 字数 199 浏览 9 评论 0原文

大家好,

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷鸟归林 2024-08-18 23:59:41

您知道您正在查找的控件的类型吗?既不是 Control 也不是 ListItem 公开 CssClass 属性,但是,ListItem 确实公开它是 Attributes 属性。

根据评论和其他问题

您应该使用System.Web.UI.HtmlControls.HtmlGenericControl

所以像这样的东西应该适合您:

HtmlGenericControl home = 
                    this.Page.Master.FindControl("list").FindControl("home")
                                                         as HtmlGenericControl;

string cssToApply = "active_navigation";

if (null != home) {
  home.Attributes.Add("class", cssToApply);
}

如果您认为有可能已经是分配给它的类,您需要附加到您可以执行以下操作:

if (null != home) {
  if (home.Attributes.ContainsKey("class")) {
    if (!home.Attributes["class"].Contains(cssToApply)){
      // If there's already a class attribute, and it doesn't already
      // contain the class we want to add:
      home.Attributes["class"] += " " + cssToApply;
    }
  }
  else {
    // Just add the new class
    home.Attributes.Add("class", cssToApply);
  }
}

如果它们不是 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:

HtmlGenericControl home = 
                    this.Page.Master.FindControl("list").FindControl("home")
                                                         as HtmlGenericControl;

string cssToApply = "active_navigation";

if (null != home) {
  home.Attributes.Add("class", cssToApply);
}

If you think there might already be an class assigned to it that you need to append to you could do something like:

if (null != home) {
  if (home.Attributes.ContainsKey("class")) {
    if (!home.Attributes["class"].Contains(cssToApply)){
      // If there's already a class attribute, and it doesn't already
      // contain the class we want to add:
      home.Attributes["class"] += " " + cssToApply;
    }
  }
  else {
    // Just add the new class
    home.Attributes.Add("class", cssToApply);
  }
}

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.

天赋异禀 2024-08-18 23:59:41

是的,在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文