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

ASP.Net
1. ADO.net Database
2. Asp Control
3. Collections
4. Components
5. Data Binding
6. Development
7. HTML Control
8. Mobile Control
9. Page
10. Request
11. Response
12. Server
13. Session Cookie
14. User Control and Master Page
15. Validation by Control
16. Validation by Function
17. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.Net » Data Binding » CheckBoxList 
Bind DataTable to asp:CheckBoxList

<%@ 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(01
        MyRow(1"Marketing"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(02
        MyRow(1"Sales"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(03
        MyRow(1"Support"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(04
        MyRow(1"Customer Service"
        MyDT.Rows.Add(MyRow)
        cbl2.DataSource = MyDT
        cbl2.DataBind()
    End If
End Sub
Sub cbl1_Clicked(sender As Object, e As EventArgs)
    Dim As Integer
    lblMessage.Text = "Preferred office color(s):<BR>"
    For i = To cbl1.Items.Count - 1
        If cbl1.Items(i).Selected Then
            lblMessage.Text = lblMessage.Text _
                & cbl1.Items(i).Text & "<BR>"
        End If
    Next
End Sub
Sub cbl2_Clicked(sender As Object, e As EventArgs)
    Dim As Integer
    lblMessage2.Text = "ID of Department(s) you work in:<BR>"
    For i = To cbl2.Items.Count - 1
        If cbl2.Items(i).Selected Then
            lblMessage2.Text = lblMessage2.Text _
                & cbl2.Items(i).Value & "<BR>"
        End If
    Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>CheckBoxList 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(s):"
/>
<asp:CheckBoxList 
    id="cbl1" 
    runat="server"
    CellPadding="5"
    CellSpacing="5"
    RepeatColumns="3"
    RepeatDirection="Vertical"
    RepeatLayout="Table"
    TextAlign="Right"
    AutoPostBack="True"
    OnSelectedIndexChanged="cbl1_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>Gold</asp:ListItem>
</asp:CheckBoxList>
<HR>
<asp:Label
    id="lblMessage2"
    runat="server"
    Font-Bold="True"
    Text="ID of Department(s) you work in:<BR>"
/>
<BR>
<asp:CheckBoxList 
    id="cbl2" 
    runat="server"
    AutoPostBack="True"
    OnSelectedIndexChanged="cbl2_Clicked"
    DataTextField="DepartmentName"
    DataValueField="DepartmentID"
    BackColor = "LightYellow"
    ForeColor = "DarkRed"
    BorderColor = "DarkBlue"
    BorderStyle = 8
    TextAlign="Left"
    RepeatLayout="Table"
>
</asp:CheckBoxList>

</Font>
</Form>
</BODY>
</HTML>
           
       
Related examples in the same category
w_w__w_.___j_a_v_a2__s_.c__om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.