Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Creating fieldset, ol/ul and li tags in ASP.Net, C#

I need to write an ASP.Net form which will produce the following HTML:

<fieldset>   
<legend>Contact Details</legend>   
<ol>   
<li>   
<label for="name">Name:</label>   
<input id="name" name="name" class="text" type="text" />   
</li>   
<li>   
<label for="email">Email address:</label>   
<input id="email" name="email" class="text" type="text" />   
</li>   
<li>   
<label for="phone">Telephone:</label>   
<input id="phone" name="phone" class="text" type="text" />   
</li>   
</ol>   
</fieldset>  

However, the fields which are to be added to the form will be determined at runtime, so I need to create the fieldset at runtime and add an ordered list and listitems to it, with labels, textboxes, checkboxes etc as appropriate.

I can’t find standard ASP.Net objects which will create these tags.

For instance, I’d like to do something like the following in C#:

FieldSet myFieldSet = new FieldSet();
myFieldSet.Legend = “Contact Details”;
OrderedList myOrderedList = new OrderedList();

ListItem listItem1 = new ListItem();
ListItem listItem2 = new ListItem();
ListItem listItem3 = new ListItem();

// code here which would add labels and textboxes to the ListItems

myOrderedList.Controls.Add(listItem1);
myOrderedList.Controls.Add(listItem2);
myOrderedList.Controls.Add(listItem3);

myFieldSet.Controls.Add(myOrderedList);

Form1.Controls.Add(myFieldSet);

Are there any standard ASP.Net objects which can produce this, or is there some other way of achieving the same result?

Matt

like image 358
Matt Avatar asked Mar 01 '11 12:03

Matt


1 Answers

You can try this:

Panel myFieldSet = new Panel();
myFieldSet.GroupingText= “Contact Details”;
HtmlGenericControl myOrderedList = new HtmlGenericControl("ol");

HtmlGenericControl listItem1 = new HtmlGenericControl ("li");
HtmlGenericControl listItem2 = new HtmlGenericControl ("li");
HtmlGenericControl listItem3 = new HtmlGenericControl ("li");

// code here which would add labels and textboxes to the ListItems

myOrderedList.Controls.Add(listItem1);
myOrderedList.Controls.Add(listItem2);
myOrderedList.Controls.Add(listItem3);

myFieldSet.Controls.Add(myOrderedList);

Form1.Controls.Add(myFieldSet);
like image 174
Edwin de Koning Avatar answered Oct 07 '22 05:10

Edwin de Koning