ASP.Net/ADO.net Database/CheckBoxList
Bind data source to asp:CheckBoxList
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server">
Function AllPublishers() As System.Data.SqlClient.SqlDataReader Dim connectionString As String = "server="(local)\NetSDK"; trusted_connection=true; Database="pubs"" Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString) Dim queryString As String = "SELECT [publishers].* FROM [publishers]" Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection) sqlConnection.Open Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.rumandBehavior.CloseConnection) Return dataReader End Function Sub Page_Load(sender As Object, e As EventArgs) Page.DataBind() End Sub
</script> <html> <head> </head> <body>
<form runat="server">
<asp:CheckBoxList id="CheckBoxList1" runat="server" DataSource="<%# AllPublishers() %>" DataTextField="pub_name" DataValueField="pub_id"></asp:CheckBoxList>
<asp:RadioButtonList id="RadioButtonList1" runat="server" DataSource="<%# AllPublishers() %>" DataTextField="pub_name" DataValueField="pub_id"></asp:RadioButtonList>
<asp:DropDownList id="DropDownList1" runat="server" DataSource="<%# AllPublishers() %>" DataTextField="pub_name" DataValueField="pub_id"></asp:DropDownList>
<asp:ListBox id="ListBox1" runat="server" DataSource="<%# AllPublishers() %>" DataTextField="pub_name" DataValueField="pub_id"></asp:ListBox>
<asp:Repeater id="Repeater1" runat="server" DataSource="<%# AllPublishers() %>">
<HeaderTemplate>
Publisher List:
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "pub_name") %> (ID: <%# DataBinder.Eval(Container.DataItem, "pub_id") %>) <%# DataBinder.Eval(Container.DataItem, "city") %>, <%# DataBinder.Eval(Container.DataItem, "state") %>, <%# DataBinder.Eval(Container.DataItem, "country") %>
</ItemTemplate>
</asp:Repeater>
<asp:DataList id="DataList1" runat="server" DataSource="<%# AllPublishers() %>"> <ItemTemplate> <p> ID: <asp:Label id="Label6" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "pub_id") %>"></asp:Label> Name: <asp:Label id="Label7" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "pub_name") %>"></asp:Label>
Address: <asp:Label id="Label8" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "city") %>"></asp:Label> , <asp:Label id="Label9" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "state") %>"></asp:Label> , <asp:Label id="Label10" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "country") %>"></asp:Label>
</ItemTemplate> <HeaderTemplate> <asp:Label id="Label1" runat="server" Font-Names="Tahoma" Font-Italic="True">List of publishers:</asp:Label>
</HeaderTemplate> <FooterTemplate>
</FooterTemplate> <SeparatorTemplate>
</SeparatorTemplate> </asp:DataList> </p> </form>
</body> </html>
</source>