How to Use RadioButton One Example in Asp.Net C#

RADIOBUTTON


The RadioButton control is used to display a radio button.


DEMO


                            Html Coding


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr><td></td><td>
            <asp:Label ID="Label1" runat="server" Text="Select Your Hobby"></asp:Label>
            </td></tr>
        <tr><td></td><td>
            <asp:RadioButton ID="RadioButton1"
                runat="server" AutoPostBack="True"  GroupName="A"
                 OnCheckedChanged="RadioButton1_CheckedChanged1" Text="Playing" />
            </td></tr>
        <tr><td></td><td>
            <asp:RadioButton ID="RadioButton2"
                 runat="server" AutoPostBack="True" GroupName="A"
                 OnCheckedChanged="RadioButton2_CheckedChanged" Text="Singing" />
            </td></tr>
        <tr><td></td><td>
            <asp:RadioButton ID="RadioButton3"
                 runat="server" AutoPostBack="True" GroupName="A"
                 OnCheckedChanged="RadioButton3_CheckedChanged" Text="Writing" />
            </td></tr>
        <tr><td></td><td>
            <asp:RadioButton ID="RadioButton4"
                runat="server" AutoPostBack="True" GroupName="A"
                 OnCheckedChanged="RadioButton4_CheckedChanged" Text="Reading" />
            </td></tr>
        <tr><td></td><td>
            <asp:RadioButton ID="RadioButton5"
                runat="server" AutoPostBack="True" GroupName="A"
                 OnCheckedChanged="RadioButton5_CheckedChanged" Text="Browsing" />
            </td></tr>
        <tr><td></td><td>
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            </td></tr>


   </table>
      
    </div>
    </form>
</body>
</html>           

C# Coding



protected void RadioButton1_CheckedChanged1(object sender, EventArgs e)
    {
        Label2.Text = " your Hobby is Playing ";
    }
    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        Label2.Text = " your Hobby is Singing";
    }
   
   
    protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
    {
        Label2.Text = " your  Hobby is Writing";
    }
  
    protected void RadioButton4_CheckedChanged(object sender, EventArgs e)
    {
        Label2.Text = "your Hobby is Reading";
    }
  
   
    protected void RadioButton5_CheckedChanged(object sender, EventArgs e)
    {
        Label2.Text = "your Hobby is Browsing";
    }






if you set it's GroupName property same for multiple radiobuttons then all radiobuttons with same name act as a single group. 


To support radio buttons, the .NET Framework provides the RadioButton control. This control is implemented by the RadioButton class defined in the System.Web.UI.WebControls  namespace.
To visually create a radio control, from the Standard section of the Toolbox, drag a RadioButton and drop it on the form. To manually create a radio button, create an Asp:RadioButton tag.




PropertyDescription
AutoPostBackA Boolean value that specifies whether the form should be posted immediately after the Checked property has changed or not. Default is false
CheckedA Boolean value that specifies whether the radio button is checked or not
idA unique id for the control
GroupNameThe name of the group to which this radio button belongs
OnCheckedChangedThe name of the function to be executed when the Checked property has changed
runatSpecifies that the control is a server control.  Must be set to "server"
TextThe text next to the radio button
TextAlignOn which side of the radio button the text should appear (right or left)






First - Add new WebForm - Select Label & Radio Buttons








Next - Select Radio Button Go to  Property window - Text Change









Next - Select Radio Button Go to  Property window - AutoPostBack = True (for every Radio Button)







Next - Select Radio Button Go to  Property window - GroupName=A(for every Radio Button Same Name)









Next - Select Radio button  Double Click Write Coding






















                        
                           Next - Go to - Run program


OUTPUT





Next - Select one Radio Button  Display  Output 

Alternately Select RadioButton 





















0 comments:

Post a Comment