Reading a checkbox in a Gridview TemplateField.

M

mercercreek

This one should be easy. Hope someone has a clue.

Simple Scenario: Gridview with mulitple rows, each row with a checkbox.
The user checks boxes of her choice. Clicks a button on the form (not
in the grid). Desired aciton is then taken in response to the
button_onclick event relevant to the row in which the checked
checkboxes exist.

Unfortunate Outcome: Each checkbox returns false for its
attribute:checked whether the user has placed a check-mark in it or
not, and does not get to participate in ViewState, but instead reverts
to having no check after the PostBack.

Here's the Grid:

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" />
<Columns>
...

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="CheckBox1"/>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>

Here's the hopeless code:

Attempt #1:
GridViewRowCollection rows = GridView1.Rows;
foreach (GridViewRow row in rows)
{
str +=
((CheckBox)row.Cells[0].FindControl("CheckBox1")).Checked.ToString()
}


Attemp#2:
foreach (GridViewRow row in rowColl)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (null != cb && (cb.Checked))
str = "The silly thing is checked!";
}

Attempt#3
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows;
bool isChecked =
((CheckBox)row.FindControl("CheckBox1")).Checked;

if (isChecked)
{
str = "The silly thing is checked!";
}
}

The value of str never indicates that the checkbox is recognized as
being checked.

I note that the checked checkboxes are however posted with the form. On
PostBack I can process the form and the presense of checked boxes is
detectable:

foreach (string name in Request.Form)
{
string temp = Request.Form[name];

if(name.IndexOf("CheckBox1") > -1)
{
sRow += name + "=" + Request.Form[name] + "<BR />";
}
}

But "GridView1$ctl02$chkOne = on" is useless information apart
from the other values in the Gridview row. What is needed are the rows
the user has selected just prior to the button_onclick event. Any ideas?
 
M

mercercreek

OK, I figured it out. Here's an answer.

The trick is, if you want a checkbox on every row that you can process
when the form is submitted with respect to the Grieview row - then DO
NOT to put a checkbox control in the ItemTemplate. Put a Literal
control there instead. Then jump in at the RowDataBound event on the
Gridview and assign a NAME that will correspond to the row in the
Gridview like this:

protected void gvQuoteList_RowDataBound(Object sender,
GridViewRowEventArgs e)
{
Literal lit1 = (Literal)e.Row.FindControl("Literal1");

string sControl = "<input type='checkbox' name =
'chkPrint" + e.Row.DataItemIndex + "'";
lit1.Text = sControl;




Then later and you'll be able later to determine in which rows the user
chose to enter a check in the box.
My intention is to grab a value from a specific cell in the GridView
row for where the checkbox is "on"
like this:



foreach (GridViewRow row in gvQuoteList.Rows)
{
Literal r = (Literal)row.FindControl("Literal1");

RequestString = "chkPrint" + row.DataItemIndex;
RequestValue = Request.Form[RequestString];

if (RequestValue == "on")
{
thethingiwaslookingfor = row.Cells[1].Text;



Still no ViewState, so if you want the checks to persist you'll have to
keep track of 'em.

This one should be easy. Hope someone has a clue.

Simple Scenario: Gridview with mulitple rows, each row with a checkbox.
The user checks boxes of her choice. Clicks a button on the form (not
in the grid). Desired aciton is then taken in response to the
button_onclick event relevant to the row in which the checked
checkboxes exist.

Unfortunate Outcome: Each checkbox returns false for its
attribute:checked whether the user has placed a check-mark in it or
not, and does not get to participate in ViewState, but instead reverts
to having no check after the PostBack.

Here's the Grid:

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" />
<Columns>
...

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="CheckBox1"/>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>

Here's the hopeless code:

Attempt #1:
GridViewRowCollection rows = GridView1.Rows;
foreach (GridViewRow row in rows)
{
str +=
((CheckBox)row.Cells[0].FindControl("CheckBox1")).Checked.ToString()
}


Attemp#2:
foreach (GridViewRow row in rowColl)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (null != cb && (cb.Checked))
str = "The silly thing is checked!";
}

Attempt#3
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows;
bool isChecked =
((CheckBox)row.FindControl("CheckBox1")).Checked;

if (isChecked)
{
str = "The silly thing is checked!";
}
}

The value of str never indicates that the checkbox is recognized as
being checked.

I note that the checked checkboxes are however posted with the form. On
PostBack I can process the form and the presense of checked boxes is
detectable:

foreach (string name in Request.Form)
{
string temp = Request.Form[name];

if(name.IndexOf("CheckBox1") > -1)
{
sRow += name + "=" + Request.Form[name] + "<BR />";
}
}

But "GridView1$ctl02$chkOne = on" is useless information apart
from the other values in the Gridview row. What is needed are the rows
the user has selected just prior to the button_onclick event. Any ideas?
 

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

Top