Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am using VS2008,C#.net and Asp.net web application.

How can use radio button in web page. I am using 2 radio button. only one radio button should checked at a time.

while i checked radio button, both are checked at a time. hope u will understand my problem. how can resolve this issue.

any link or suggestion will be appreciated
Regards
Mukesh
Posted
Updated 13-Apr-12 19:36pm
v2

Radiobuttons are grouped together by using the GroupName property[^].
Set the same GroupName for both the radio buttons.
Another example here[^].
 
Share this answer
 
Hi,
check this code.
XML
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1-transitional.dtd">

<script runat="server">
    protected void RadioButton_CheckedChanged(object sender,System.EventArgs e) {
        if (RadioButton1.Checked == true)
        {
            Response.Write("You Selected: Asp.Net");
        }
        else {
            Response.Write("You Selected: ColdFusion");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>RadioButton example: how to use RadioButton control in asp.net</title>
    </head>

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:RadioButton ID="RadioButton1" runat="server" Text="Asp.Net" GroupName="Software" AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" />
                <asp:RadioButton ID="RadioButton2" runat="server" Text="ColdFusion" GroupName="Software" AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" />
            </div>
        </form>
    </body>
</html>


Best Luck
Happy Coding
 
Share this answer
 
HI Mukesh,

Try groupname property of radiobuton. Hopes it will surely help you out.


Ajinkya S.
Web Developer.
 
Share this answer
 
Hi Mukesh

The Solution by Abhinav is very clear and right way so you go for Abhinav's solution.

Select Radio Button and set groupname propert (like-k) for both radio button from property window.

Best Regards
Kuldeep
 
Share this answer
 
Hi,
check this code.


XML
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1-transitional.dtd">

<script runat="server">

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedValue == "a")
        {
            Response.Write("You Selected: Asp.Net");
        }
        else
        {
            Response.Write("You Selected: ColdFusion");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>RadioButton example: how to use RadioButton control in asp.net</title>
    </head>

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" Width="146px"
                    AutoPostBack="True"
                    onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
                    <asp:ListItem Value="a">Asp .Net</asp:ListItem>
                    <asp:ListItem Value="b">coldFusion</asp:ListItem>
                </asp:RadioButtonList>
                <br />
            </div>
        </form>
    </body>
</html>
 
Share this answer
 
hi,
mukesh
first you take two radio button..radiobutton1 and radiobutton2..

then you r to select first radiobutton and go on the properties of radiobutton1...then you will have to select 'group name' who is in the properties of radiobutton..then u r to give a group name of radobutton1..for example- a.

then this process should b revised in radiobutton2..
but group name should b same of radiobutton1 and radiobutton2..


ankur joshi
ankurjoshi03@yahoo.com
 
Share this answer
 
v2
Comments
CHill60 12-Aug-13 10:16am    
Which is the same as Solutions 1, 3 and 4 a year ago.
Word of advice - don't post your email in posts in forums as you can end up getting spammed
to validate a group of radio buttons


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>




XML
<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.
       }
   }
 
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