How to bind checkbox in asp.net

Posted by Shail12345 under ASP.NET on 11/5/2013 | Points: 10 | Views : 2069 | Status : [Member] | Replies : 4
Hi Sir,
How to bind checkbox in dyanmicaly and if any checkbox click event fire then filter data in
table show in page;
for ex;
Emp Table
Name,Roll,Phone(column)
if check name click event then filter data all Emp table in page
thanks




Responses

Posted by: Bandi on: 11/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Means you have column names in the Checkbox (List Items are Name, Roll, Phone)
If you select(check) NAME in the checkbox you should query like SELECT NAME FROM TableName ; For Roll check you should query only rolls from the table( SELECT Roll FROM TableName)...

Is this your requirement?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shail12345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,
Refer the following link for binding CheckedListBox with database table
http://www.webcodeexpert.com/2013/04/how-to-fill-checkboxlist-from-sql.html#.UnnUZXCnoRo

SelecteIndexChanged event will be fired whenever the item selected in the CheckedListBox

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shail12345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Sample code for binding CheckedListBox and then filter gridView data based on Checked Item


    <form id="form1" runat="server">

<div>
<asp:CheckBoxList ID="cblDepts" runat="server" RepeatColumns="2" AutoPostBack="true" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"></asp:CheckBoxList>
<asp:GridView runat="server" id="gdBindCheckBox"/>
</div>
</form>

        protected void Page_Load(object sender, EventArgs e)

{
if (!Page.IsPostBack)
{
FillQualCheckBoxList();
}

}

private void FillQualCheckBoxList()
{
SqlConnection con = new SqlConnection("Data source=XXXX; Initial Catalog=study; Integrated Security=true");
SqlCommand cmd = new SqlCommand("Select DISTINCT DEPARTMENT_ID from EMPLOYEES WHERE DEPARTMENT_ID IS NOT NULL ", con); // Binding CheckedListBox with Department IDs
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
cblDepts.DataSource = dt;
cblDepts.DataTextField = "DEPARTMENT_ID";
cblDepts.DataValueField = "DEPARTMENT_ID";
cblDepts.DataBind();
}


// This event will be fired whenever the item selection changes in the check box
protected void CheckBoxList1_SelectedIndexChanged (Object sender, EventArgs e)
{
// Getting selected item value from check box
string str = cblDepts.SelectedItem.ToString() ;
string connectionstring = "Data source=XXXXX; Initial Catalog=study; Integrated Security=true";

SqlConnection conn = new SqlConnection(connectionstring);
conn.Open();
SqlCommand comm = new SqlCommand("SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, DEPARTMENT_ID FROM [dbo].[EMPLOYEES] WHERE DEPARTMENT_ID IN (" + str + ");", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
gdBindCheckBox.DataSource = ds;
gdBindCheckBox.DataBind();
}



References:
http://www.dotnetfox.com/articles/how-to-bind-data-to-checkboxlist-control-in-Asp-Net-using-C-Sharp-1042.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shail12345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi you can bind your check box with multiple ways :-

1). Using Datareader:-

Sqlconnection con = new SqlConnection("Your connection string");
con.open();
SqlCommand cmd = new SqlCommand(con);
cmd.commandtext = "select emp_id,emp_name from employee_master where status = 'AA'";
SqlDataReader dr = cmd.ExecuteReader();

chk_lst_emp.Items.Clear();
While(dr.read())
{
try
{
if(!IsDBNull(dr("emp_name"))
chk_lst_emp.Iteams.Add(New ListItem(convert.tostring(dr("emp_id")),convert.tostring(dr("emp_name")));
}
catch(exception ex)
{
throw ex;
}
finally
{
if(dr!=null) dr.close();
if(con!=null) con.close();
}
}
2). Using DataSet:-

Sqlconnection con = new SqlConnection("Your connection string");
//no need to open a connection because dataset is a disconnected architecture
SqlCommand cmd = new SqlCommand(con);
cmd.commandtext = "select emp_id,emp_name from employee_master where status = 'AA'";
SqlDataAdapter da = new SqlDataAdapter(cmd);
Dataset ds = new Dataset();
da.Fill(ds,"Employee_Master");
if(ds.tables(0).rows.count>0)
{
chk_lst_emp.DataSource = ds;
chk_lst_emp.DataTextField = "emp_name";
chk_lst_emp.DataValueField = "emp_id";
chk_lst_emp.DataBind();
}
else
{
chk_lst_emp.DataSource = null;
chk_lst_emp.DataBind();
}

3). Using List(Generics) for this you need to create separate class and make properties like

Public Class Employee_Master
{
private string emp_id = string.empty;
private string emp_name = string.empty;

public string Employee_Id
{
get{return emp_id;}
set{emp_id = value;}
}
//make same for emp_name
}

Public void fill_checkbox_list()
{
//take list as
List lst_employee = new List(Employee_Master);
Employee_Master emp_obj = null;

Sqlconnection con = new SqlConnection("Your connection string");
con.open();
SqlCommand cmd = new SqlCommand(con);
cmd.commandtext = "select emp_id,emp_name from employee_master where status = 'AA'";
SqlDataReader dr = cmd.ExecuteReader();

chk_lst_emp.Items.Clear();

While(dr.read())
{
try
{
if(!IsDBNull(dr("emp_name"))
{
emp_obj = new Employee_Master();
emp_obj.Employee_Id = convert.tostring(dr("emp_id"));
emp_obj.Employee_Name = convert.tostring(dr("emp_name"))

lst_employee.Add(emp_obj);
}
}
catch(exception ex)
{
throw ex;
}
finally
{
if(dr!=null) dr.close();
if(con!=null) con.close();
}
}
}
//after while loop close
if(lst_employee.Count>0)
{
chk_lst_emp.DataSource = lst_employee;
chk_lst_emp.DataTextField = "emp_name";
chk_lst_emp.DataValueField = "emp_id";
chk_lst_emp.DataBind();
}
else
{
chk_lst_emp.DataSource = null;
chk_lst_emp.DataBind();
}
}

//You can also fill you checkbox list with distionary in that case you need to give
DataTextField = "key" and DataValueField = "value"

I think it helps you lot.

Shail12345, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response