473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading a checkbox in a Gridview TemplateField.


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[i];
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?

Oct 26 '06 #1
1 21433
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.
mercercreek wrote:
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[i];
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?
Nov 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: cook.jonathan.m | last post by:
I want to programmatically add a dropdown to each row in a certain column returned to the gridview by my query. I want to do this entirely in behind-code. My gridview tag in the page will have...
0
by: Pemmavasu | last post by:
Hi All, I am not able to read the values from Gridview TemplateField Controls. basically I craeted a gridview with four colums and finally i added a Template field to it. The templatefield...
1
by: Stan SR | last post by:
Hi, How can I convert this in c# code ? <asp:TemplateField HeaderStyle="100"> <ItemTemplate> <a href="#" onclick="<%#funcDelete(Container.DataItem)%>">Delete</a> </ItemTemplate>...
1
by: jobs | last post by:
I've been staring at this and can't explain why it's not working. The grid shows up fine, except the last column, Reset User button does not render??? I checked the codebehind and I am not...
4
by: HibernatingBear | last post by:
Hello, I've been driving myself nuts trying to find a solution to this. I'm still a relative newbie to C#, and would be grateful for any helpful suggestions. I'm trying to find a way to...
1
by: mghihor | last post by:
Dear GridView Experts! I have problems with GridView when adding a column using TemplateField. I fill GridView1 from a stored procedure. It has a Select statement and returns 14 columns,...
1
by: Danielle | last post by:
Greetings all and thanks in advance for any help you can provide. I am trying ultimately to get values from a gridview row when a checkbox on the row is checked. the problem I'm having is that...
7
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Hi all, this is a second post, so apologies, but I never had an answer to my first post (several weeks ago) and I really need some help. I'm using a .Net 2.0 Gridview which is populated using an...
1
by: k3nz0 | last post by:
Hi All, I have a GridView and inside this GridView I only have one TemplateField. Inside this TemplateField, I have plenty of textboxes. Values of these textboxes are extracted from the database....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.