Search

Wednesday, September 18, 2013
0 comments

How to bind CheckBoxList using SqlDataSource in ASP.NET

7:28 AMWednesday, September 18, 2013

SqlDataSource 

The SqlDataSource control is a DataSource control that allows a server control, Such as the GridView control, to access Data located in a relational database, such as Microsoft SQL Server and Oracle.
This control also generates a connection string to interact with data in an ASP.NET page. These connection strings are generated through the Configure Data Source wizard. A Connection String contains informattion about the server, database , and security you are working with in your application , for example
Data Source= .\SqlExpress; Initial Catalog =Database name ; Integrated Security = True
The SqlDataSource control automatically opens a database connection , which is provided in the connection string, executes the SQL queries , and then close the connection. When you add a SqlDataSource control to an ASP.NET page , the following code appears in the HTML code of the page.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"> </asp:SqlDataSource>

CheckBoxList 

You can use a CheckBoxList control to display a number of check boxes at once as a column of check boxes . The CheckBoxList control exists within the System.Web.UI.WebControls namespace. This control is often useful when you want to bind the data from a data source to checkboxes . The CheckBoxList control creates multiselection checkbox groups at runtime by binding these control to a data source.

Lets take an example for binding CheckBoxList using SqlDataSource
Step-1: Drop CheckBoxList control to design window 
Step-2: Choose DataSource from showSmart tag ..... 
Step-3:  Same as GridView Binding

Complete Source code




<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
        <asp:CheckBoxList ID="CheckBoxList1" runat="server"
            DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="id">
        </asp:CheckBoxList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT * FROM [mytab]"></asp:SqlDataSource>
  
    </div>
    </form>
</body>
</html>

Output
How to bind CheckBoxList using SqlDataSource in ASP.NET

Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top