RadioButtonList ASP.NET Server Control

RadioButtonList ASP.NET Server Control"-->

only for RuBoard

RadioButtonList ASP.NET Server Control

The RadioButtonList control's programmatic functionality is nearly identical to the CheckBoxList , but its functionality is different. When rendered, each item from the RadioButtonList.DataSource will be part of a group of RadioButton controls and only one RadioButton out of the group can ever be selected at one time.

The RadioButtonList is part of the List controls suite, so its behavior is the same as the proceeding List controls as far as how you enable automatic post back ( AutoPostBack ), how you tell if an item is checked ( ListItem.Selected ), and how to handle the SelectedIndexChanged on the post back. So, not to sound redundant, I will be jumping right into the code example. Listing 8.22 illustrates how to use all the proceeding attributes and events to determine item selection.

Listing 8.22 Using the RadioButtonList Control
 [VisualBasic.NET] 01: <%@ Import Namespace="System.Data" %> 02: <%@ Import Namespace="System.Data.SqlClient" %> 03: <script runat="server" language="vb" > 04: 05:  public sub Page_Load(sender as Object, e as EventArgs) 06: 07:   if (not IsPostBack) then 08:    rbl_DataBind() 09:   end if 10: 11:  end sub 12: 13:  public sub rbl_DataBind() 14: 15:   dim SqlCon as new SqlConnection("server=localhost; uid=sa;pwd=;database=northwind") 16:   dim SqlCmd as new SqlCommand("SELECT TOP 21 ProductName, ProductID FROM Products", graphics/ccc.gif SqlCon) 17: 18:   SqlCon.Open() 19:   rbl.DataSource = SqlCmd.ExecuteReader(CommandBehavior.CloseConnection) 20:   rbl.DataTextField = "ProductName" 21:   rbl.DataValueField = "ProductID" 22:   rbl.DataBind() 23: 24:  end sub 25: 26:  public sub RadioButtonList_SelectedIndexChanged(sender as Object, e as EventArgs) 27: 28:   dim sb as new StringBuilder("<b><u>Items Selected</u></b><p>") 29: 30:   dim i as integer 31:   for i = 0 to rbl.Items.Count - 1 32: 33:    if(rbl.Items(i).Selected) then 34: 35:     sb.Append(i) 36:     sb.Append(" - ") 37:     sb.Append(rbl.Items(i).Text) 38:     sb.Append("<br>") 39: 40:    end if 41: 42:   next 43: 44:   lCheckBoxList.Text = sb.ToString() 45: 46:  end sub 47: 48: </script> [C#.NET] 01: <%@ Import Namespace="System.Data" %> 02: <%@ Import Namespace="System.Data.SqlClient" %> 03: <script runat="server" language="C#" > 04: 05:  void Page_Load(Object sender, EventArgs e) { 06: 07:   if (! IsPostBack) { 08:    rbl_DataBind(); 09:   } 10: 11:  } 12: 13:  void rbl_DataBind(){ 14: 15:   SqlConnection SqlCon = new SqlConnection("server=localhost; graphics/ccc.gif uid=sa;pwd=;database=northwind"); 16:   SqlCommand SqlCmd = new SqlCommand("SELECT TOP 21 ProductName, ProductID FROM graphics/ccc.gif Products", SqlCon); 17: 18:   SqlCon.Open(); 19:   rbl.DataSource = SqlCmd.ExecuteReader(CommandBehavior.CloseConnection); 20:   rbl.DataTextField = "ProductName"; 21:   rbl.DataValueField = "ProductID"; 22:   rbl.DataBind(); 23: 24:  } 25: 26:  void RadioButtonList_SelectedIndexChanged(Object sender, EventArgs e) { 27: 28:   StringBuilder sb = new StringBuilder("<b><u>Item Selected</u></b><p>"); 29: 30:   int i; 31:   for(i = 0; i < rbl.Items.Count; i++){ 32: 33:    if(rbl.Items[i].Selected) { 34: 35:     sb.Append(i); 36:     sb.Append(" - "); 37:     sb.Append(rbl.Items[i].Text); 38:     sb.Append("<br>"); 39: 40:    } 41: 42:   } 43: 44:   lCheckBoxList.Text = sb.ToString(); 45: 46:  } 47: 48: </script> [VisualBasic.NET & C#.NET] 49: <html> 50:  <body> 51:   <form runat="server"> 52: 53:     <asp:Label 54:      width="100%" 55:      runat="server" 56:      text="<center>Pick Products</center>" 57:      BackColor="white" 58:      ForeColor="Navy" 59:      Font-Bold="true" 60:      Font-Size="13" 61:     /> 62: 63:     <asp:RadioButtonList 64:      runat="server" 65:      id="rbl" 66: 67:      CellPadding="4" 68:      CellSpacing="0" 69: 70:      RepeatLayout="table" 71:      RepeatColumns="3" 72:      RepeatDirection="Vertical" 73: 74:      AutoPostBack="true" 75:      OnSelectedIndexChanged="RadioButtonList_SelectedIndexChanged" 76: 77:      font-size="10" 78:      BackColor="white" 79:      ForeColor="Navy" 80:      Font-Bold="true" 81:      width="100%" 82:      BorderWidth="1" 83:      BorderColor="Navy" 84:     /> 85: 86:     <p> 87: 88:     <asp:label 89:      runat="server" 90:      id="lCheckBoxList" 91:      Font-Bold="false" 92:      Font-Size="8" 93:      ForeColor="Navy" 94:      /> 95: 96: </form> 97: </body> 98: </html> 

When Listing 8.22 is executed, you'll get a page with three columns of RadioButton controls and a product name next to each. When you select one, the page is posted back to the server and the name of the product you selected is printed out to the screen. Unlike the CheckBoxList you can only check one product at a time when using the RadioButtonList control. Figure 8.12 contains an illustration of the page after an item is selected and the page is posted back to the server.

Figure 8.12. The RadioButtonList control.
graphics/08fig12.gif
only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net