2011-03-01 41 views
11

我需要寫一個ASP.Net形式,將產生以下HTML:編程方式創建字段集,OL/UL和ASP.Net李標籤,C#

<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> 

然而,這些領域是添加到表單將在運行時確定,因此我需要在運行時創建字段集,並向其添加有序列表和列表項,並根據需要添加標籤,文本框,複選框等。

我無法找到將創建這些標記的標準ASP.Net對象。

舉例來說,我想這樣做在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); 

是否有能產生這一點,或任何標準的ASP.Net的對象是有實現相同的其他方式結果?

馬特

回答

26

你可以試試這個:

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); 
+0

Thanks,Rewinder。這很好。而不是面板,我使用以下控件:http://dotnetslackers.com/articles/aspnet/BuildingGroupBoxControl.aspx - 再次感謝。 – Matt 2011-03-01 14:29:49

+0

這很有幫助。創建ol,創建li。將li添加到ol。將ol添加到面板。 – IMAbev 2011-07-27 13:20:00

0

根據上面的答案,我有這樣的情況:一個數組被證明是有用的,如下所示。注意:我在id =「ulNoTree」的aspx代碼中創建了一個無序列表。

int NumFloorsCt = 10; 
LinkButton[] rgBL; 
HtmlGenericControl[] rgLI; 

/// <summary> 
/// set up an array of LinkButtons with "li" controls 
/// - each LinkButton click is handled by the same event handler 
/// </summary> 
void SetUpLinkButtons(List<FLOOR> listFloorRecs) 
{ 
    NumFloorsCt = 10; 
    rgBL = new LinkButton[NumFloorsCt]; 
    rgLI = new HtmlGenericControl[NumFloorsCt]; 

    for (int i = 0; i < NumFloorsCt; i++) 
    { 
    rgBL[i] = new LinkButton(); 
    rgBL[i].ID = LB_ID_prefix + listFloorRecs[i].ID; 
    rgBL[i].Click += new System.EventHandler(LB_fp_Click); 
    rgBL[i].Text = listFloorRecs[i].DESCRIP; 
    rgBL[i].ToolTip = "Click here to display floor info"; 

    rgLI[i] = new HtmlGenericControl("li"); 
    rgLI[i].Controls.Add(rgBL[i]); 

    ulNoTree.Controls.Add(rgLI[i]); 
    } 
} 

/// <summary> 
/// event handler for any of the link buttons 
/// </summary> 
protected void LB_fp_Click(object sender, EventArgs e) 
{ 
    LinkButton btn = (LinkButton)(sender); 
    // do your action here 
} 
相關問題