2014-02-12 143 views
0
<asp:RadioButton ID="RadioButton1" runat="server" Text="A" GroupName="rbn" /> 
<asp:RadioButton ID="RadioButton2" runat="server" Text="B" GroupName="rbn" /> 
<asp:RadioButton ID="RadioButton3" runat="server" Text="C" GroupName="rbn"/> 
<asp:RadioButton ID="RadioButton4" runat="server" Text="D" GroupName="rbn"/> 
<asp:Button ID="Button1" runat="server" Text="Validate" OnClick="Button1_Click" /> 
<asp:Label ID="Label1" runat="server" Text=""></asp:Label> 

在點擊按鈕Validate它應該驗證單選按鈕組rbn並顯示在Label1託運單選按鈕文本。在按鈕單擊驗證單選按鈕組在C#

這應該在C#中完成。請不要使用任何腳本和for循環。

幫我出這個問題,並感謝提前

+1

-1不表明你已經嘗試過,資本化每個單詞的第一個字母。 –

+0

@DeeMac先生這是我第一次嘗試 – Selva

回答

1

您可以使用RadioButtonList代替RadioButton。驗證RadioButtonList使用內置驗證控件RequiredFieldValidator來驗證整個列表。

.aspx

<asp:RadioButtonList ID="RadioButtonList1" RepeatColumns="2" 
    RepeatDirection="Vertical" RepeatLayout="Table" runat="server"> 
    <asp:ListItem>A</asp:ListItem> 
    <asp:ListItem>B</asp:ListItem> 
    <asp:ListItem>C</asp:ListItem> 
    <asp:ListItem>D</asp:ListItem> 
</asp:RadioButtonList> 
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" 
    ControlToValidate="RadioButtonList1" Text="Required"> 
</asp:RequiredFieldValidator> 

<asp:Button ID="Button1" runat="server" Text="Validate" OnClick="Button1_Click" /> 
<asp:Label ID="Label1" runat="server" Text=""></asp:Label> 

.aspx.cs:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    Label1.Text = "You selected: "; 
    Label1.Text += RadioButtonList1.SelectedItem.Text.ToString(); 
} 

注:RequiredFieldValidator必須RadioButtonList(而不是每個列表項)。

UPDATE:

,因爲你需要RadioButtons代替RadioButtonList,試試這個:

HTML

<input type="radio" value="A" name="radiodbtn" runat="server" /> A 
<input type="radio" value="B" name="radiodbtn" runat="server" /> B 
<input type="radio" value="C" name="radiodbtn" runat="server" /> C 
<input type="radio" value="D" name="radiodbtn" runat="server" /> D 

<asp:CustomValidator runat="server" ID="validateCheckBoxes" EnableClientScript="true" 
OnServerValidate="validateCheckBoxes_ServerValidate" 
OnClientValidate="validateCheckBoxes_ClientValidate">Required</asp:CustomValidator> 

然後添加JavaScript進行客戶端驗證:

<script type="text/javascript"> 
function CheckBoxRequired_ClientValidate(sender, e) 
{ 
    e.IsValid = $("input[name='radiodbtn']").is(':checked'); 
} 
</script> 

現在添加服務器端驗證:

protected void validateCheckBoxes_ServerValidate(object sender, ServerValidateEventArgs e) 
{ 
e.IsValid = RadioButton1.Checked || RadioButton2.Checked || RadioButton3.Checked || RadioButton4.Checked; 
if(e.IsValid) 
{ 
// at least any one radio button is checked among all group 
} 
else 
{ 
// no radio button is checked among all group 
} 
+0

沒有驗證它應該做 – Selva

+0

@Selva:對不起,我忘了從元素中刪除'ValidationGroup =「rbn」'。現在所有設置(答案更新),現在它應該工作!如果不是,請告訴我。 –

+0

先生,我說過,它不應該在單選按鈕列表先生。 – Selva

1

您可以使用單選按鈕列表,而不是單選框組。下面是示例代碼

<asp:RadioButtonList ID="rbList" runat="server"> 
<asp:ListItem Text="Option1" Value="A" Selected="True"></asp:ListItem> // Selected attribute is used to select a default value 
<asp:ListItem Text="Option2" Value="B"></asp:ListItem> 
</asp:RadioButtonList> 

在代碼隱藏

protected void btnGetValue_Click(object sender, EventArgs e) 
{ 
    string OptionName = rbList.SelectedValue; 
} 

你會得到選擇的選項值到OPTIONNAME變量

+0

不需要列表項目對不起....... – Selva

+0

如果你不需要列表項目,那麼你應該使用if else條件來知道哪個單選按鈕被檢查。但我建議使用RadioButtoList –