Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VS2008 C# ASP.NET
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 9-Apr-12 21:45pm
Edited 13-Apr-12 20:36pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Radiobuttons are grouped together by using the GroupName property[^].
Set the same GroupName for both the radio buttons.
Another example here[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
check this code.
<%@ 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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

Ajinkya S.
Web Developer.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hi,
check this code.
 

<%@ 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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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
  Permalink  
v2
Comments
CHill60 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

to validate a group of radio buttons
 

<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>
 

 
<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>
 

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

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

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 CPallini 155
3 PIEBALDconsult 150
4 BillWoodruff 148
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 12 Aug 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100