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;
}
}


thnx it was a grt help!!!!!!
ReplyDelete