San
San

Reputation: 1837

Enclosing listbox inside DIV tag

I want to enclose listbox inside a DIV tag in the code behind file using C#.I am adding all the required attributes and adding the list box as below:

Controls.Add(listBox);

I want the generated listbox to be enclosed with in a div. Please suggest how this can be done.

Thanks in advance.

Upvotes: 2

Views: 653

Answers (1)

Andy Rose
Andy Rose

Reputation: 16984

If the div isn't specified in the form then it can be created in the code behind using the HtmlGeneric class:

HtmlGenericControl myDiv = new HtmlGenericControl { TagName = "div" };
myDiv.Controls.Add(listBox);
this.form1.Controls.Add(myDiv);

Otherwise if the div is already in the HTML just add an id and the runat="server" attibute and add it direclty

HTML:

<div id="myDiv" runat="server"></div>

code behind:

myDiv.Controls.Add(listBox);

Upvotes: 1

Related Questions