tags:

views:

79

answers:

1

Hi,

I am not sure if is it possible or not but I have to change some classes of <li> tag which I am getting from ascx file.Now the condition which li to change or no is based on html that I am producing with the help of xsl(written in code behind). I know i can do this with the help of Javascript. But what if I don't wanna use javascript and want to do it wth the help of code behind itself.Is this possible let me explain this with the example also

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

I am getting A,B,C from the user control using Datagrid for it. now In this user control I have xslt and from this xslt I am getting html A,B.... and I want to change classes of li tag of A and B to sth else now.Can I do it withot using javacript. I want to write sth in my codebehind to acheive this...Can anyone tell me how is it possible?? any idea....

A: 

As you said, this has to be done in the code-behind if you aren't willing to use javascript on the client-side.

There are many ways to do this in code. Your best option may be to create generic html controls, which you can apply classes to.

If you're processing a li element, you can create a list item generic html control and apply a class to it via:

//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);

Hope this helps.

KP
this seems to be a nice approach but tell me one thing...I am already using ascx control to do produce by layout of A,B,C,D and these are not static they are dynamic coming from based on query to database.So I have given in ascx like this:<li><%#Container.DataItem%></li> and in code behind I am binding my datasource to it. So now how I will know that which li tag is for A,which is for B....
AB
In that case you may need to change your ascx layout to use server side controls. You can bind them as usual, but hook into the ondatabound event in the code behind and apply the class dynamically. Your other option is to hook into the on databound event of the data source itself, and to process/parse the template to find the items dynamically and apply classes. Personally I'd try the first way.
KP