2011-05-31 39 views
0

我有一個HtmlGenericController,併爲此我想添加RadioButtons。我的單選按鈕來自RadioButtonList,因此對象是Listitems。我如何讓我的通用控制器顯示單選按鈕?c#.net如何讓一個HtmlGenricControll顯示一個RadioButtonList項目

這是我的代碼

private HtmlGenericControl generateCells(String domainName) 
     { 
      HtmlGenericControl container = new HtmlGenericControl("div"); 

      HtmlGenericControl dName = new HtmlGenericControl("span"); 
      dName.InnerHtml = domainName; 

      RadioButtonList radioList = new RadioButtonList(); 
      radioList.ID = "radio_" + domainName; 
      radioList.RepeatDirection = RepeatDirection.Horizontal; 


      ListItem sunriseA = new ListItem(); 
      sunriseA.Value = Price_Types.SUNRISE_ONE.ToString(); 
      //sunriseA.Text = Price_Types.SUNRISE_ONE.ToString(); 
      sunriseA.Text = ""; 
      radioList.Items.Add(sunriseA); 


      ListItem sunriseB = new ListItem(); 
      sunriseB.Value = Price_Types.SUNRISE_TWO.ToString(); 
      //sunriseB.Text = Price_Types.SUNRISE_TWO.ToString(); 
      sunriseB.Text = ""; 
      radioList.Items.Add(sunriseB); 

      ListItem landrush = new ListItem(); 
      landrush.Value = Price_Types.LANDRUSH.ToString(); 
      //landrush.Text = Price_Types.LANDRUSH.ToString(); 
      landrush.Text = ""; 
      radioList.Items.Add(landrush); 

      ListItem general = new ListItem(); 
      general.Value = Price_Types.GENERAL.ToString(); 
      //general.Text = Price_Types.GENERAL.ToString(); 
      general.Text = ""; 
      radioList.Items.Add(general); 

      container.Controls.Add(dName); 

      foreach (ListItem item in radioList.Items) 
      { 
       HtmlGenericControl span = new HtmlGenericControl("span"); 
       span.InnerHtml = item;//what to put here?? 
       container.Controls.Add(span); 
      } 


      return container; 

     } 

回答

2
var radioButton = new HtmlGenericControl("input"); 
radioButton.Attributes["type"] = "radio"; 
radioButton.Attributes["name"] = "groupName"; 
radioButton.Attributes["value"] = "buttonValue"; 

這隻會呈現圓形單選按鈕本身,雖然。要添加標籤,除了它之外,您還必須呈現一個span。或者,IIRC,呈現一個label字段,並將for屬性設置爲單選按鈕的ID,因此單擊該標籤也會自動單擊該按鈕。

+0

Okej聽起來不錯,我怎麼才能找到一個回傳組名時,我想「保存」選定的收音機?我有15-20組。 – Marthin 2011-05-31 08:20:52

相關問題