18

I have several <li> elements with different id's on ASP.NET page:

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

and can change their class using JavaScript like this:

li1.className="class2"

But is there a way to change <li> element class using ASP.NET? It could be something like:

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

But FindControl() doesn't work as I expected. Any suggestions?

Thanks in advance!

Alexander Prokofyev
  • 33,874
  • 33
  • 95
  • 118

8 Answers8

30

Add runat="server" in your HTML page

then use the attribute property in your asp.Net page like this

li1.Attributes["Class"] = "class1";
li2.Attributes["Class"] = "class2";
user49845
  • 301
  • 3
  • 2
12

FindControl will work if you include runat="server" in the <li>

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

Otherwise you server side code can't 'see' it.

GateKiller
  • 74,180
  • 73
  • 171
  • 204
naspinski
  • 34,020
  • 36
  • 111
  • 167
  • 3
    Don't forget that when you add the runat="server" to the LI element, you'll need to modify the JavaScript to find the element using the server control's ClientID property – Stephen Wrighton Sep 23 '08 at 13:00
5

The FindControl method searches for server controls. That is, it looks for controls with the attribute "runat" set to "server", as in:

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

Because your <li> tags are not server controls, FindControl cannot find them. You can add the "runat" attribute to these controls or use ClientScript.RegisterStartupScript to include some client side script to manipulate the class, e.g.

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());
5

This will find the li element and set a CSS class on it.

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.

cweston
  • 11,297
  • 19
  • 82
  • 107
4

you must set runat="server" like:

<li id="li1" runat="server">stuff</li>
Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
stefano m
  • 4,094
  • 5
  • 28
  • 27
3

Please try this if you want to apply style:

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

For CSS, you can try below syntax :

li1.Attributes.Add("class", "clsItem");
j0k
  • 22,600
  • 28
  • 79
  • 90
Vimal Patel
  • 81
  • 1
  • 3
1

Leaf Dev provided the solution for this, but in the place of "ctl" you need to insert "Master".

It's working for me anyway:

using System.Web.UI.HtmlControls;

HtmlGenericControl liItem = (HtmlGenericControl) ctl.FindControl("liItemID");
liItem.Attributes.Add("class", "someCssClass");
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

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");
ebram khalil
  • 8,252
  • 7
  • 42
  • 60