473,785 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# checkbox - what am I doing wrong?


Anyone know why the code below does not work. All I want is to check a
checkbox, then hit a button, then for the value of the DataKeyField in
the DataGrid to show up as the Text property in a Label. The DataGrid is
working OK as if the foreach loop, the bit where its coming unstuck is
the check box definition or test to see if it is checked. I don't get a
compilation error - just that it doesn't work.

<asp:DataGrid id="DataGrid1" DataKeyField="R equestID" runat="server"
Font-Size="X-Small">
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:CheckBox ID="checkboxSel ect"
Runat="server"> </asp:CheckBox>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>

</asp:DataGrid>
private void Button1_Click(o bject sender, System.EventArg s e)
{
StringBuilder sb = new StringBuilder() ;
foreach(DataGri dItem DGridItem in DataGrid1.Items )
{

CheckBox myCheckbox =
(CheckBox)DGrid Item.FindContro l("checkboxSele ct");

if(myCheckbox.C hecked == true)
{
sb.Append(DataG rid1.DataKeys[DGridItem.ItemI ndex].ToString());
}
}
Label.Text = sb.ToString();
}
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
3 22845
Guess the check boxes are getting reset everytime the page is called
back. In your page Page_Load method, are you checking if the page is a
postback?

HTH,
-Azhagan

"Joe Bloggs" <bo********@net scape.net> wrote in message
news:uB******** ******@TK2MSFTN GP11.phx.gbl...

Anyone know why the code below does not work. All I want is to check a
checkbox, then hit a button, then for the value of the DataKeyField in
the DataGrid to show up as the Text property in a Label. The DataGrid is
working OK as if the foreach loop, the bit where its coming unstuck is
the check box definition or test to see if it is checked. I don't get a
compilation error - just that it doesn't work.

<asp:DataGrid id="DataGrid1" DataKeyField="R equestID" runat="server"
Font-Size="X-Small">
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:CheckBox ID="checkboxSel ect"
Runat="server"> </asp:CheckBox>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>

</asp:DataGrid>
private void Button1_Click(o bject sender, System.EventArg s e)
{
StringBuilder sb = new StringBuilder() ;
foreach(DataGri dItem DGridItem in DataGrid1.Items )
{

CheckBox myCheckbox =
(CheckBox)DGrid Item.FindContro l("checkboxSele ct");

if(myCheckbox.C hecked == true)
{
sb.Append(DataG rid1.DataKeys[DGridItem.ItemI ndex].ToString());
}
}
Label.Text = sb.ToString();
}
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Hi,

Are you finding the checkbox?
If not you will get an exception in the if

if you find it and it's never checked then they are not keeping the status,
check if you do a databind() in a postback, if so you have to change it
like this:

void Page_Load(... )
{
if ( !IsPostBack )
this.DataBind() ;
}

Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Joe Bloggs" <bo********@net scape.net> wrote in message
news:uB******** ******@TK2MSFTN GP11.phx.gbl...

Anyone know why the code below does not work. All I want is to check a
checkbox, then hit a button, then for the value of the DataKeyField in
the DataGrid to show up as the Text property in a Label. The DataGrid is
working OK as if the foreach loop, the bit where its coming unstuck is
the check box definition or test to see if it is checked. I don't get a
compilation error - just that it doesn't work.

<asp:DataGrid id="DataGrid1" DataKeyField="R equestID" runat="server"
Font-Size="X-Small">
<Columns>
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:CheckBox ID="checkboxSel ect"
Runat="server"> </asp:CheckBox>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>

</asp:DataGrid>
private void Button1_Click(o bject sender, System.EventArg s e)
{
StringBuilder sb = new StringBuilder() ;
foreach(DataGri dItem DGridItem in DataGrid1.Items )
{

CheckBox myCheckbox =
(CheckBox)DGrid Item.FindContro l("checkboxSele ct");

if(myCheckbox.C hecked == true)
{
sb.Append(DataG rid1.DataKeys[DGridItem.ItemI ndex].ToString());
}
}
Label.Text = sb.ToString();
}
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Hi - yes I was missing the IsPostBack check on my Page_Load method.
Thank you for your help.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

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

Similar topics

2
1456
by: Graham Mattingley | last post by:
I am trying to count the records in a database, and make an array from the answer this is the code strQuery = "SELECT count(*) FROM resort where island_id= '"&island_drop&"' ;" Set rsIslandinfo = adoConn.Execute(strQuery) i = rsIslandinfo("count(*)").Value ** this bit above here works find I have done a response.write to check I have a number in i
4
5617
by: Simom Thorpe | last post by:
Hi, I'm trying to insert a line into a MS access DB using ASP on IIS 5. This is the line: con.execute "INSERT INTO newProds(title,desc,catcode) VALUES ('Champagne Muff Scarf','','AC304B')" But it throws up this error:
2
2711
by: Aaron Ackerman | last post by:
I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried everything and I am at a loss. The using goes into add mode with the add button adds his data then updates with the update button, seems simple. I am using ALL visual controls (supposedly to simplify things. If I was not using the visual controls and calling an ExecuteNonQuery no prob. Please look at my code and tell me what I am doing wrong. Also, what are the advatages...
2
1804
by: Phil Certain | last post by:
Hi, Relative newbie to .Net but experienced with classic ASP. I am trying to create a simple business object to contain commonly used functions. This is what I have done: 1 - Created a simple vb file containing: Imports System.Data.SqlClient Imports System.Web.HttpUtility
2
1413
by: shapper | last post by:
Hello, I have an ASP.NET page and in its runtime code I am trying to get an user profile, change it and save it. It works if I use Profile, which is the profile for the current authenticated user. But I want to change the profile of another user. Anyway, I am using:
2
1832
by: alnoir | last post by:
I've looked around online and have even had a friend help me, however, for some reason I can't compare two strings. I'm doing this at the end of the code (within the two foreach loops), above where the 'print "found"' code is located. It works when I use a string rather than the scalar variable '$word'. However, this just isn't feasible in the program that is being developed. If anyone sees anything that I'm doing wrong, I'd really...
8
2069
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the vertices and can do this multiple times on a sinlge vertex cluster. The problem I have been encoutering is that, if I select a second vertex cluster and try to edit that , the program crashes.
0
1326
by: shapper | last post by:
Hello, I am creating a class with a control. I compiled the class and used it on an Asp.Net 2.0 web site page. I can see the begin and end tags of my control (<oland </ol>) but somehow the child controls (just a literal for testing) of my control is not being added to the page. Could someone tell me what am I doing wrong?
16
1915
by: SirG | last post by:
I'm looking for an explanation of why one piece of code works and another does not. I have to warn you that this is the first piece of Javascript I've ever written, so if there is a better way or a simpler answer, by all means show me the light! What I'm trying to do is refresh the page at a timed interval ( actually redirect the page... ) and I have a simple piece of code I got from the net that works, but I need to modify it a little...
10
1801
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
0
10319
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
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
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.