C#

Monday, February 3, 2014

RadioButton List control in asp.net using c#

Introduction

The RadioButtonList control provides page developers with a single-selection radio button group that can be dynamically generated through data binding. It contains anItems collection with members that correspond to individual items on the list. To determine which item is selected, test the SelectedItem property of the list.

Specifying List Layout


You can specify how the list is rendered by setting the RepeatLayout and RepeatDirection properties. For information about layout options, see the RepeatLayoutenumeration.
By default, RepeatDirection is set to RepeatDirection.Vertical. Setting this property to RepeatDirection.Horizontal causes the control to render the list horizontally. SomeRepeatLayout settings do not allow horizontal layout. For more information, see the RepeatLayout enumeration.
This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. You can use validation controls to verify user input before displaying the input text in a RadioButtonListcontrol. ASP.NET provides an input request validation feature to block script and HTML in user input.

Example:

The following code example demonstrates how to programmatically modify the display of a RadioButtonList control.
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>RadioButtonList Example</title>
<script language="C#" runat="server">

        void Button1_Click(object Source, EventArgs e) 
        {
           if (RadioButtonList1.SelectedIndex > -1) 
           {  
              Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
           }
        }

        void chkLayout_CheckedChanged(Object sender, EventArgs e) 
        {

           if (chkLayout.Checked == true) 
           {
              RadioButtonList1.RepeatLayout = RepeatLayout.Table;
           }
           else 
           {
              RadioButtonList1.RepeatLayout = RepeatLayout.Flow;
           }     
        }

        void chkDirection_CheckedChanged(Object sender, EventArgs e) 
        {

           if (chkDirection.Checked == true) 
           {
              RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
           }
           else 
           {
              RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;
           }  
        }

     </script>

 </head>
 <body>

     <h3>RadioButtonList Example</h3>

     <form id="form1" runat="server">

         <asp:RadioButtonList id="RadioButtonList1" runat="server">
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Item 2</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
            <asp:ListItem>Item 5</asp:ListItem>
            <asp:ListItem>Item 6</asp:ListItem>
         </asp:RadioButtonList>

         <br />

         <asp:CheckBox id="chkLayout" OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout" Checked="true" AutoPostBack="true" runat="server" />

         <br />

         <asp:CheckBox id="chkDirection" OnCheckedChanged="chkDirection_CheckedChanged" Text="Display Horizontally" AutoPostBack="true" runat="server" />

         <br />

         <asp:Button id="Button1" Text="Submit" onclick="Button1_Click" runat="server"/>

         <br />

         <asp:Label id="Label1" font-names="Verdana" font-size="8pt" runat="server"/>

     </form>

 </body>
 </html>
   

RadioButtonList Example

      You selected: Item 3

No comments:

Post a Comment