ASP.Net/Data Binding/RadioButtonList

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Bind DataTable to asp:RadioButtonList

   <source lang="csharp">

<%@ Page Language=VB Debug=true %> <%@ Import Namespace="System.Data" %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)

   If Not IsPostBack Then
       Dim MyDT As New DataTable
       Dim MyRow As DataRow
       MyDT.Columns.Add(New DataColumn("DepartmentID", _
           GetType(Int32)))
       MyDT.Columns.Add(New DataColumn("DepartmentName", _
           GetType(String)))
       MyRow = MyDT.NewRow()
       MyRow(0) = 1
       MyRow(1) = "Marketing"
       MyDT.Rows.Add(MyRow)
       MyRow = MyDT.NewRow()
       MyRow(0) = 2
       MyRow(1) = "Sales"
       MyDT.Rows.Add(MyRow)
       MyRow = MyDT.NewRow()
       MyRow(0) = 3
       MyRow(1) = "Support"
       MyDT.Rows.Add(MyRow)
       MyRow = MyDT.NewRow()
       MyRow(0) = 4
       MyRow(1) = "Customer Service"
       MyDT.Rows.Add(MyRow)
       rbl2.DataSource = MyDT
       rbl2.DataBind()
       rbl2.Items(3).Selected=True
   End If

End Sub Sub rbl1_Clicked(sender As Object, e As EventArgs)

   lblMessage.Text = "Preferred office color:
" _ & rbl1.SelectedItem.Text & "
"

End Sub Sub rbl2_Clicked(sender As Object, e As EventArgs)

   lblMessage2.Text = "ID of Department you work in:
" _ & rbl2.SelectedItem.Value & "
"

End Sub </SCRIPT> <HTML> <HEAD> <TITLE>RadioButtonList Control Sample Page</TITLE> </HEAD> <BODY > <form runat="server"> <asp:Label

   id="lblMessage"
   runat="server"
   Font-Bold="True"
   Text="Preferred office color:"

/> <asp:RadioButtonList

   id="rbl1" 
   runat="server"
   CellPadding="5"
   CellSpacing="5"
   RepeatColumns="3"
   RepeatDirection="Vertical"
   RepeatLayout="Table"
   TextAlign="Right"
   AutoPostBack="True"
   OnSelectedIndexChanged="rbl1_Clicked"

>

   <asp:ListItem>Blue</asp:ListItem>
   <asp:ListItem>Red</asp:ListItem>
   <asp:ListItem>Green</asp:ListItem>
   <asp:ListItem>Purple</asp:ListItem>
   <asp:ListItem>Black</asp:ListItem>
   <asp:ListItem Selected>Gold</asp:ListItem>

</asp:RadioButtonList>


<asp:Label

   id="lblMessage2"
   runat="server"
   Font-Bold="True"
   Text="ID of Department you work in:
"

/>
<asp:RadioButtonList

   id="rbl2" 
   runat="server"
   AutoPostBack="True"
   OnSelectedIndexChanged="rbl2_Clicked"
   DataTextField="DepartmentName"
   DataValueField="DepartmentID"
   BackColor = "LightYellow"
   ForeColor = "DarkRed"
   BorderColor = "DarkBlue"
   BorderStyle = 8
   TextAlign="Left"
   RepeatLayout="Table"

> </asp:RadioButtonList> </Form> </BODY> </HTML>

      </source>