I have check box list in a page. I generate values for this check box list from database in pageload as follows:
CheckBoxList2.DataSource = objRegLogic.GetSPList("SP_ Allergy");
CheckBoxList2.DataTextField = "name";
CheckBoxList2.DataValueField = "pkey";
CheckBoxList2.DataBind();
I want to add these checkboxlist value in to database in a button click.I use the following code.But when debugging " if (CheckBoxList2.Items[i]. Selected == true)" this condition can't work properly. Even i checked one item it doesn't enter in to the loop
Can u please check this code,whether i did any wrong??
protected void labtestbtn_Click(object sender, EventArgs e)
{
ConsultingEntity objConsEntity = new ConsultingEntity();
PatientConsultingBLLLogic objPatConsBLL = new PatientConsultingBLLLogic();
string Pid = Request.QueryString["PID"] as string;
if (Pid == null || Pid == string.Empty)
{
Pid = ddlPatient.SelectedValue;
}
string DocID = Convert.ToString(Session[" pkey"]);
if (DocID == null || DocID == string.Empty)
{
DocID = ddldoc.SelectedValue;
}
try
{
if (Pid == "")
{
}
else
{
for (int i = 0; i < CheckBoxList2.Items.Count; i++)
{
if (CheckBoxList2.Items[i]. Selected == true)
{
string labtest = item.Value;
objConsEntity.LabTest = labtest;
objConsEntity.Doctorid = Convert.ToInt16(DocID);
objConsEntity.PatientId = Convert.ToInt16(Pid);
objPatConsBLL. InsertPatientLapTest( objConsEntity);
}
}
}
}
catch (Exception ex)
{
}
}
I didn't understand where is the wrong. Can anyone help me to do this?

Rajesh Kumar JhaPosted Nov 6, 2013, 1:50 AM
On PageLoad You have Bind the CheckBoxList ,
This Code Should be surrounded by " if(!IsPostBack) "
e.g.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CheckBoxList1.DataSource = obj;
CheckBoxList1.DataTextField = "Name";
CheckBoxList1.DataValueField = "Id";
CheckBoxList1.DataBind();
}
}
Pls Mark it as answer if it solves the issue!