简体   繁体   English

如何使用ASP.NET更改HTML页面元素的CSS类?

[英]How to change CSS class of a HTML page element using ASP.NET?

I have several <li> elements with different id's on ASP.NET page: 我在ASP.NET页面上有几个具有不同id的<li>元素:

<li id="li1" class="class1">
<li id="li2" class="class1">
<li id="li3" class="class1">

and can change their class using JavaScript like this: 并可以使用这样的JavaScript更改他们的类:

li1.className="class2"

But is there a way to change <li> element class using ASP.NET? 但有没有办法使用ASP.NET更改<li>元素类? It could be something like: 它可能是这样的:

WebControl control = (WebControl)FindControl("li1");
control.CssClass="class2";

But FindControl() doesn't work as I expected. 但FindControl()不能像我预期的那样工作。 Any suggestions? 有什么建议?

Thanks in advance! 提前致谢!

Add runat="server" in your HTML page 在HTML页面中添加runat="server"

then use the attribute property in your asp.Net page like this 然后像这样在asp.Net页面中使用attribute属性

li1.Attributes["Class"] = "class1";
li2.Attributes["Class"] = "class2";

FindControl will work if you include runat="server" in the <li> 如果在<li>中包含runat =“server”,FindControl将起作用

<li id="li1" runat="server">stuff</li>

Otherwise you server side code can't 'see' it. 否则,服务器端代码无法“看到”它。

This will find the li element and set a CSS class on it. 这将找到li元素并在其上设置CSS类。

using System.Web.UI.HtmlControls;

HtmlGenericControl liItem = (HtmlGenericControl) ctl.FindControl("liItemID");
liItem.Attributes.Add("class", "someCssClass");

Remember to add your runat="server" attribute as mentioned by others. 请记住添加其他人提到的runat="server"属性。

你必须设置runat =“server”,如:

<li id="li1" runat="server">stuff</li>

The FindControl method searches for server controls. FindControl方法搜索服务器控件。 That is, it looks for controls with the attribute "runat" set to "server", as in: 也就是说,它查找属性“runat”设置为“server”的控件,如:

<li runat="server ... ></li>

Because your <li> tags are not server controls, FindControl cannot find them. 由于<li>标记不是服务器控件,因此FindControl无法找到它们。 You can add the "runat" attribute to these controls or use ClientScript.RegisterStartupScript to include some client side script to manipulate the class, eg 您可以将“runat”属性添加到这些控件,或使用ClientScript.RegisterStartupScript包含一些客户端脚本来操作类,例如

System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language=\"javascript\">");
sb.Append("document.getElementById(\"li1\").className=\"newClass\";")
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "MyScript", sb.ToString());

Please try this if you want to apply style: 如果你想申请风格,请试试这个:

li1.Style.Add("background-color", "black");

For CSS, you can try below syntax : 对于CSS,您可以尝试以下语法:

li1.Attributes.Add("class", "clsItem");

Leaf Dev provided the solution for this, but in the place of "ctl" you need to insert "Master". Leaf Dev为此提供了解决方案,但在“ctl”的位置,您需要插入“Master”。

It's working for me anyway: 无论如何它对我有用:

using System.Web.UI.HtmlControls;

HtmlGenericControl liItem = (HtmlGenericControl) ctl.FindControl("liItemID");
liItem.Attributes.Add("class", "someCssClass");

You also can try this too if u want to add some few styles: 如果你想添加一些样式,你也可以试试这个:

li1.Style.add("color","Blue");
li2.Style.add("text-decoration","line-through");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM