Click here to Skip to main content
16,020,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want at least one radiobutton to be checked. My asp code looks like

XML
<asp:RadioButtonList ID="rbtOdgovor" runat="server"
                           RepeatLayout="Table" AutoPostBack="True" CausesValidation="True">
                           <asp:ListItem Text="yes" Value="rbtYes"></asp:ListItem>
                           <asp:ListItem Text="No" Value="rbtNo"></asp:ListItem>

                       </asp:RadioButtonList>
                       <asp:RequiredFieldValidator
                           ID="ValidatorOdgovor"
                           runat="server"
                           ControlToValidate="rbtOdgovor"
                           ErrorMessage="Please enter a value; ValidateRequestMode="Enabled"></asp:RequiredFieldValidator>



ASP.NET
<asp:Button runat="server" ID="btnSave" Text="Answer" Font-Names="Tahoma" Height="30px" Width="100px"  OnClick="btnSave_Click" ValidateRequestMode="Enabled" />

But it doesnt work!!
Posted
Updated 13-Apr-14 22:51pm
v3

XML
<asp:RadioButtonList
    ID="RadioButtonList1"
    runat="server"
    RepeatColumns="3">
    <asp:ListItem>Red</asp:ListItem>
    <asp:ListItem>Yellow</asp:ListItem>
    <asp:ListItem>Blue</asp:ListItem>
    <asp:ListItem>Green</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator
    ID="ReqiredFieldValidator1"
    runat="server"
    ControlToValidate="RadioButtonList1"
    ErrorMessage="You must Select your favorite color!">*
</asp:RequiredFieldValidator>
 
Share this answer
 
Hi,

The simplest way is as the above approach from Jos. Using a RadioButtonList and a RequiredFieldValidator.
XML
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
            <asp:ListItem>north</asp:ListItem>
            <asp:ListItem>west</asp:ListItem>
        </asp:RadioButtonList>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ControlToValidate="RadioButtonList1" ErrorMessage="RequiredFieldValidator">
        </asp:RequiredFieldValidator>

2. Without RadioButtonList and if you want a group radiobutton insteads, you can also implement it by CustomValidator.
C#
<script language="javascript" type="text/javascript" >
function CustomValidator1_ClientValidate(source,args)
{
    if(document.getElementById("<%= RadioButton1.ClientID %>").checked || document.getElementById("<%= RadioButton2.ClientID %>").checked)
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }

}
//-->
</script>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:RadioButton ID="RadioButton1" runat="server" GroupName="location" Text="north" />
    <asp:RadioButton ID="RadioButton2" runat="server" GroupName="location" Text="west" />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <asp:CustomValidator id="CustomValidator1" runat="server" Display="Dynamic" ErrorMessage="please choose" ClientValidationFunction="CustomValidator1_ClientValidate" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
    </div>
    </form>
</body>


C#
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
    args.IsValid = RadioButton1.Checked || RadioButton2.Checked;
}
protected void Button1_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        //validate is successful.
    }
}


code blocks corrected
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900