problem reading the status of a check box in a gridview

M

ma

Hello,

I have a GrideView that has 4 col. 3 of them are bound to a dataset and
the last one is not. The last one is check box. The check box is defined as
follow:



<asp:TemplateField HeaderText="Select">

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" />

</ItemTemplate>

</asp:TemplateField>



I am trying to read the state of these checkbox using the folllwng code:



// Select the checkboxes from the GridView control

for (int i = 0; i < GridView_Enterprise_name.Rows.Count; i++)

{

GridViewRow row = GridView_Enterprise_name.Rows;

bool isChecked =
((CheckBox)row.FindControl("chkSelect")).Checked;

if (isChecked)

{

// do something

}

}



but the code doesn't work as isChecked is always false. What is the problem
and how can I solve it?



Regards
 
M

ma

Hello,
There is a button on the page and I am doing this on the postback event
of this button.

Regards

Eliyahu Goldin said:
When do you do that? On the first page load or on postbacks? In what
event?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


ma said:
Hello,

I have a GrideView that has 4 col. 3 of them are bound to a dataset
and the last one is not. The last one is check box. The check box is
defined as follow:



<asp:TemplateField HeaderText="Select">

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" />

</ItemTemplate>

</asp:TemplateField>



I am trying to read the state of these checkbox using the folllwng code:



// Select the checkboxes from the GridView control

for (int i = 0; i < GridView_Enterprise_name.Rows.Count; i++)

{

GridViewRow row = GridView_Enterprise_name.Rows;

bool isChecked =
((CheckBox)row.FindControl("chkSelect")).Checked;

if (isChecked)

{

// do something

}

}



but the code doesn't work as isChecked is always false. What is the
problem and how can I solve it?



Regards

 
E

Eliyahu Goldin

Is ViewState enabled for the gridview?

Make sure you are not re-loading the grid on every postback.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


ma said:
Hello,
There is a button on the page and I am doing this on the postback event
of this button.

Regards

Eliyahu Goldin said:
When do you do that? On the first page load or on postbacks? In what
event?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


ma said:
Hello,

I have a GrideView that has 4 col. 3 of them are bound to a dataset
and the last one is not. The last one is check box. The check box is
defined as follow:



<asp:TemplateField HeaderText="Select">

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" />

</ItemTemplate>

</asp:TemplateField>



I am trying to read the state of these checkbox using the folllwng code:



// Select the checkboxes from the GridView control

for (int i = 0; i < GridView_Enterprise_name.Rows.Count; i++)

{

GridViewRow row = GridView_Enterprise_name.Rows;

bool isChecked =
((CheckBox)row.FindControl("chkSelect")).Checked;

if (isChecked)

{

// do something

}

}



but the code doesn't work as isChecked is always false. What is the
problem and how can I solve it?



Regards


 
M

ma

Thanks. The problem solved.
I set the datagride data source on page_load event and it seems that doing
this will reset the checkboxes. I used a ispostback and the problem solved
now.

Regards

Eliyahu Goldin said:
Is ViewState enabled for the gridview?

Make sure you are not re-loading the grid on every postback.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


ma said:
Hello,
There is a button on the page and I am doing this on the postback event
of this button.

Regards

Eliyahu Goldin said:
When do you do that? On the first page load or on postbacks? In what
event?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Hello,

I have a GrideView that has 4 col. 3 of them are bound to a dataset
and the last one is not. The last one is check box. The check box is
defined as follow:



<asp:TemplateField HeaderText="Select">

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" />

</ItemTemplate>

</asp:TemplateField>



I am trying to read the state of these checkbox using the folllwng
code:



// Select the checkboxes from the GridView control

for (int i = 0; i < GridView_Enterprise_name.Rows.Count; i++)

{

GridViewRow row = GridView_Enterprise_name.Rows;

bool isChecked =
((CheckBox)row.FindControl("chkSelect")).Checked;

if (isChecked)

{

// do something

}

}



but the code doesn't work as isChecked is always false. What is the
problem and how can I solve it?



Regards


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top