473,748 Members | 11,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:check ed 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"
AutoGenerateCol umns="False" />
<Columns>
...

<asp:TemplateFi eld>
<ItemTemplate >
<asp:CheckBox runat="server" ID="CheckBox1"/>
</ItemTemplate>
</asp:TemplateFie ld>
...
</Columns>
</asp:GridView>

Here's the hopeless code:

Attempt #1:
GridViewRowColl ection rows = GridView1.Rows;
foreach (GridViewRow row in rows)
{
str +=
((CheckBox)row. Cells[0].FindControl("C heckBox1")).Che cked.ToString()
}
Attemp#2:
foreach (GridViewRow row in rowColl)
{
CheckBox cb = (CheckBox)row.F indControl("Che ckBox1");
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("Ch eckBox1")).Chec ked;

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$ctl0 2$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 21469
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_Row DataBound(Objec t sender,
GridViewRowEven tArgs e)
{
Literal lit1 = (Literal)e.Row. FindControl("Li teral1");

string sControl = "<input type='checkbox' name =
'chkPrint" + e.Row.DataItemI ndex + "'";
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.Row s)
{
Literal r = (Literal)row.Fi ndControl("Lite ral1");

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

if (RequestValue == "on")
{
thethingiwasloo kingfor = 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:check ed 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"
AutoGenerateCol umns="False" />
<Columns>
...

<asp:TemplateFi eld>
<ItemTemplate >
<asp:CheckBox runat="server" ID="CheckBox1"/>
</ItemTemplate>
</asp:TemplateFie ld>
...
</Columns>
</asp:GridView>

Here's the hopeless code:

Attempt #1:
GridViewRowColl ection rows = GridView1.Rows;
foreach (GridViewRow row in rows)
{
str +=
((CheckBox)row. Cells[0].FindControl("C heckBox1")).Che cked.ToString()
}
Attemp#2:
foreach (GridViewRow row in rowColl)
{
CheckBox cb = (CheckBox)row.F indControl("Che ckBox1");
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("Ch eckBox1")).Chec ked;

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$ctl0 2$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
2253
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 the following code and no more. <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false"> </asp:GridView> Now, I have generated boundfields to add regular data without a
0
1207
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 contains 3 labels and one button while i am clicking on this button. i am getting a error message Error "Object reference not set to an instance of an object" - .NET VB
1
3274
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> </asp:TemplateField>
1
4271
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 removing visiblity or disabling anywhere??? Thanks for any help or information.
4
3264
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 create a gridview from a Web application to an automated powerpoint slide. If that is not possible, then reading the gridview items to a table created in powerpoint, all in C#. Have searched Google many times, results ranged from "can't be
1
1744
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, AutoGenerateColumns="True". When I add two columns manually to be able to start drag and drop, the drag and drop works OK, but in GridView1_PreRender() and GridView1_RowCreated() I loose values of GridView1.Rows.Cells.Text and e.Row.Cells.Text, these are ""....
1
4169
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 after ticking the checkbox, the whole page refreshes (expectedly), the checkbox is unmarked and the response.write message displayed below never displays. It follows that if I uncomment the lines that try to capture the value in cell(3) and...
7
11059
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 ObjectDataSource which calls on a method in a class. This all works fine. The Gridview has a select button automatically generated. When the user presses this I want to be able to take the data from the selected record and use it. However...
1
6173
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. One of the textboxes is called txt_CarFeatures (this is the ID). Value of this textbox is extracted from the database, using Bind("CarFeatures"). There is no problem populating the value into the textbox. My problem is how to get the value from...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.