Wednesday 7 December 2016

ASP.NET - Populate Group CheckBoxList from SQLDataSource

Posted by : Manav Pandya

 How to populate CheckBoxeList from SqlDataSource in ASP.NET ?


Populate CheckBoxeList with SqlDataSource in ASP.NET 


  • As we know CheckBoxList is a Web control provided in toolbox of Visual Studio within ASP.NET project .
  • Main use of this is that we can give facility to user that they can select multiple Option as they want as per category .
  • Here in this article i am going to use SqlDataSource in ASP.NET to populate data within CheckBoxList 
  • Datasource control contains the items to display in the checkboxlist. after creating DataSource control, developers can call checkboxlist DataBind method to bind the datasource to the checkboxlist control. this method allow developers to programmatically (dynamically) data bind checkboxlist control at run time. 
  • We also can use checkboxlist (DataSourceID) property to specify datasource control using declarative syntax. declarative syntax also support DataTextField and DataValueField properties. 

Check Out My Sql Server Video On YouTube :

Sql Server : How To Generate Database Script File

Following snippet shows demo of the same 


<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
    protected void Button1_Clicked(object sender,System.EventArgs e) {
        Label1.Text = "You have Selected  this :-<br/>";
        foreach (ListItem li in CheckBoxList1.Items){
            if (li.Selected == true){
                Label1.Text += li.Text + "<br/>";
            }
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
 <title>Manav Pandya  CheckBoxList populate with SqlDataSource</title>
     </head>
      <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server"></asp:Label>
                <hr />
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>" SelectCommand="SELECT  [ProductName]  FROM [Products] ">
                </asp:SqlDataSource>
                <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="ProductName" RepeatColumns="4" BorderStyle="Solid" BorderColor="Red" BorderWidth="1px">
                </asp:CheckBoxList>
                <asp:Button ID="Button1" runat="server" Text="Vew Selected Products" OnClick="Button1_Clicked" />
                
            </div>
        </form>
    </body>
</html>

Try to run this code by yourself and make changes you want .

Thanks for reading , comment ur query if you have .
basic asp.net mvc tutorials, Free asp.net mvc Tutorials for beginners, mvc, asp.net mvc, .net tutorials, mvc tutorial, mvc tutorials, asp.net mvc tutorials, mvc tutorial, mvc tutorials for Beginners , asp.net mvc Basics, basic mvc, mvc4, mvc5,.net mvc