i get false value of checkbox

Posted by Satiss7pwr under ASP.NET on 1/19/2010 | Views : 1783 | Status : [Member] | Replies : 6
public CheckBox[] cb = null;
int z=0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cb = new CheckBox[count - k];
//Database code.........
while (dr.Read())//read data from access database
{
cb[z] = new CheckBox();
cb[z].Text = dr["Member_Name"].ToString();
Panel2.Controls.Add(cb[z]);
Panel2.Controls.Add(new LiteralControl("</br>"));
z = z + 1;
}
}
}
protected void Button6_Click(object sender, EventArgs e)
{
for (int x = 0; x < cb.Length; x++)//ERROR IS HERE:Object reference not set to an instance of an object.
{
if (cb[x].Checked == true)//EVERY TIME I GOT FALSE VALUE
{
//processing check boxes
}
}
}




Responses

Posted by: Abhi2434 on: 1/19/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
I have already told you, actually member variables will be lost whenever page is posted back. so you will receive false every time (as false is Default for Boolean)

You need to get the actual object from Panel; or use CheckBoxList declared in the Page.


www.abhisheksur.com

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

Posted by: Majith on: 1/19/2010 [Member] Starter

Up
0
Down
Good day,

While reading from the database check the following method ,it handles null and returns true or false.

 public bool CheckIsTrue(object temp)

{
if (temp== DBNull.Value)
return false;
int flag = Convert.ToInt32(temp);
if (flag == 1)
return true;
else
return false;
}


cb = new CheckBox[count - k];
could you explain what is k here.

Thanks

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

Posted by: Satiss7pwr on: 1/19/2010 [Member] Starter

Up
0
Down
count and k is variable that decide that how many check box will created

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

Posted by: Satiss7pwr on: 1/19/2010 [Member] Starter

Up
0
Down
pls give me code ,tell me where i put it,sorry im new in web


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

Posted by: Mehulthakkar1 on: 1/19/2010 [Member] Starter

Up
0
Down
just remove the condition if(!ispostback).

it will work.

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

Posted by: Nishithraj on: 1/19/2010 [Member] Bronze

Up
0
Down
The condition checking will execute if the page is not posted back now.

Change the logic as it checks always with page is loaded.

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

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

Login to post response