不使用javascript更改基于HTML的类?

时间:2010-01-08 18:48:28

标签: c# asp.net html

我不确定是否可能,但我必须更改我从ascx文件中获取的<li>标签的某些类。现在改变或不改变的条件是基于html我在xsl的帮助下生成(用后面的代码编写)。 我知道我可以借助Javascript来做到这一点。但是,如果我不想使用javascript并希望通过自身背后的代码来帮助我做这件事。这可能让我用这个例子解释一下

<li>A</li>
<li>B</li>
<li>C</li>
-----
----

我使用Datagrid从用户控件获取A,B,C。 现在在这个用户控件中我有xslt,从这个xslt我得到html A,B .... 我想把A和B的li标签改为现在的其他类。我可以使用javacript来实现它。我想写一下我的代码隐藏来实现这个......有人能告诉我怎么可能?任何想法......

1 个答案:

答案 0 :(得分:0)

正如你所说,如果你不愿意在客户端使用javascript,这必须在代码隐藏中完成。

在代码中有很多方法可以做到这一点。您最好的选择可能是创建通用的html控件,您可以将类应用到。

如果您正在处理li元素,则可以创建列表项通用html控件并通过以下方式将类应用于该元素:

//create an unordered list
HtmlGenericControl myList = new HtmlGenericControl("ul");

//create the list item
HtmlGenericControl myListItem = new HtmlGenericControl("li");
myListItem.InnerText = "A"; //you can get/set the li item contents using this property

//set the class here
myListItem.Attributes["class"] = "someClassName";

//add the list item to the list, and the list to the control set of your page, usercontrol,etc.
myList.Controls.Add(myListItem);
this.Controls.Add(myList);

希望这有帮助。