Bind DataTable to asp:RadioButtonList : RadioButtonList « Data Binding « ASP.Net






Bind DataTable to asp:RadioButtonList

<%@ 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:<BR>" _
        & rbl1.SelectedItem.Text & "<BR>"
End Sub
Sub rbl2_Clicked(sender As Object, e As EventArgs)
    lblMessage2.Text = "ID of Department you work in:<BR>" _
        & rbl2.SelectedItem.Value & "<BR>"
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>RadioButtonList Control Sample Page</TITLE>
</HEAD>
<BODY  >
<form runat="server">
<Font Face="Tahoma">
<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>
<HR>
<asp:Label
    id="lblMessage2"
    runat="server"
    Font-Bold="True"
    Text="ID of Department you work in:<BR>"
/>
<BR>
<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>

</Font>
</Form>
</BODY>
</HTML>
           
       








Related examples in the same category