Pages

Tuesday, December 27, 2011

CheckBoxList example In Asp.net c#


CheckBoxList example In Asp.net c# 

Many time we require to bind the data in checkboxlist using database sql server in asp.net . I have here provided complete Html code and Code behind code for binding CheckBoxList In Asp.net c# .Following are my simple code .

Screen Shots :-

Html Code :-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Checkboxlist.aspx.cs" Inherits="Checkboxlist" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <table>
        <tr> <td> <h2>  CheckBoxList example</h2> </td> </tr>
        <tr>
            <td> <asp:CheckBoxList ID="chklisttest" runat="server"> </asp:CheckBoxList>  </td>
        </tr>
    </table>
    </form>
</body>
</html>

Code Behind :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Checkboxlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindCheckBoxList();
        }
    }
    private void bindCheckBoxList()
    {   try
        {
            DataSet Ds = GetDataSet("Select * from Countrytable");
            chklisttest.DataSource = Ds;
            chklisttest.DataTextField = "CountryName";
            chklisttest.DataValueField = "CountryID";
            chklisttest.DataBind();
        }
        catch (Exception){}
    }
    private DataSet GetDataSet(string Query)
    {   DataSet Ds = new DataSet();
        try
        {   string strCon = @"Data Source=Servername;Initial Catalog=Test;Integrated Security=True";
            SqlConnection Con = new SqlConnection(strCon);
            SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
            Da.Fill(Ds);           
        }
        catch (Exception){}
        return Ds;
    }
}


Related Other posts

4 comments:

  1. thnx it was a grt help!!!!!!

    ReplyDelete
  2. Thanx dude it's very helpful 4 me....

    ReplyDelete
  3. 192.168.l.254.254 router login admin password informations. With http://192.168.1.254 ip address, you can perform many operations such as wifi password change, router configuration, Ip configuration, Iptv, port opening via this Ip.

    ReplyDelete
  4. Workday Training Online Free
    Master Workday HCM concepts like staffing, payroll, compensation, and reporting with free training resources. Gain practical HR automation skills and certification-oriented learning.

    ReplyDelete