code_junkie

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

Nov 11th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. <li id="li1" class="class1">
  2. <li id="li2" class="class1">
  3. <li id="li3" class="class1">
  4.  
  5. li1.className="class2"
  6.  
  7. WebControl control = (WebControl)FindControl("li1");
  8. control.CssClass="class2";
  9.  
  10. <li runat="server ... ></li>
  11.  
  12. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  13. sb.Append("<script language="javascript">");
  14. sb.Append("document.getElementById("li1").className="newClass";")
  15. sb.Append("</script>");
  16. ClientScript.RegisterStartupScript(this.GetType(), "MyScript", sb.ToString());
  17.  
  18. <li id="li1" runat="server">stuff</li>
  19.  
  20. li1.Attributes["Class"] = "class1";
  21. li2.Attributes["Class"] = "class2";
  22.  
  23. using System.Web.UI.HtmlControls;
  24.  
  25. HtmlGenericControl liItem = (HtmlGenericControl) ctl.FindControl("liItemID");
  26. liItem.Attributes.Add("class", "someCssClass");
Add Comment
Please, Sign In to add comment