2010-12-06 105 views
10

我有30個單獨的RadioButton。我無法使用RadioButtonList。有3組按鈕。每個組都有一個唯一的GroupName。一切工作正常在網絡瀏覽器。我怎樣才能知道在每個給定GroupsNames中選擇了哪個按鈕?ASP.Net c#給定GroupName中的哪個單選按鈕被選中?

編輯:功能我用

private string getRadioValue(ControlCollection clts, string groupName) 
{ 
    string ret = ""; 
    foreach (Control ctl in clts) 
    { 
     if (ctl.Controls.Count != 0) 
     { 
      if (ret == "") 
       ret = getRadioValue(ctl.Controls, groupName); 
     } 

     if (ctl.ToString() == "System.Web.UI.WebControls.RadioButton") 
     { 
      RadioButton rb = (RadioButton)ctl; 
      if (rb.GroupName == groupName && rb.Checked == true) 
       ret = rb.Attributes["Value"]; 
     } 
    } 
    return ret; 
} 

回答

8

你必須檢查所有單選按鈕選中屬性。
有沒有簡單的方法來檢查它由groupName。 (你可以寫掃描對組名的一些控制容器和返回列表中的所有單選按鈕的方法,控制檢查,但更容易是掃描所有RB)

+1

令人失望,謝謝。 – Justin808 2010-12-07 02:10:35

0

可以使用的Request.Form(「組名」)

+1

由於ASP.NET將「粗俗」添加到名稱屬性,因此這不起作用。例如:ctl00 $ cphBody $ Mthree GroupName是Mthree,如果我要使用Request.Form,我需要知道crud部分,因爲GroupName不可訪問。 – Justin808 2010-12-07 02:31:20

+0

好吧,如果您的頁面設置需要修改,通常您可以通過其中一個控件的ClientID屬性找到殘缺部分。 – joelt 2010-12-07 04:15:28

1

將相同的處理程序附加到每個RadioButton。然後檢查你正在尋找的屬性。將啓用回發設置爲true。

protected void RadioButton1_30_CheckedChanged(object sender, EventArgs e) 
{ 
    RadioButton rb = (RadioButton)sender;   
    string txtVal = rb.Text; 
    string groupName= rb.GroupName; 
    int tmpInt; 
    Int32.TryParse(txtVal, out tmpInt); 

} 
相關問題