473,382 Members | 1,635 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,382 software developers and data experts.

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="RequestID" runat="server"
Font-Size="X-Small">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="checkboxSelect"
Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

</asp:DataGrid>
private void Button1_Click(object sender, System.EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach(DataGridItem DGridItem in DataGrid1.Items)
{

CheckBox myCheckbox =
(CheckBox)DGridItem.FindControl("checkboxSelect");

if(myCheckbox.Checked == true)
{
sb.Append(DataGrid1.DataKeys[DGridItem.ItemIndex].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 22824
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********@netscape.net> wrote in message
news:uB**************@TK2MSFTNGP11.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="RequestID" runat="server"
Font-Size="X-Small">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="checkboxSelect"
Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

</asp:DataGrid>
private void Button1_Click(object sender, System.EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach(DataGridItem DGridItem in DataGrid1.Items)
{

CheckBox myCheckbox =
(CheckBox)DGridItem.FindControl("checkboxSelect");

if(myCheckbox.Checked == true)
{
sb.Append(DataGrid1.DataKeys[DGridItem.ItemIndex].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********@netscape.net> wrote in message
news:uB**************@TK2MSFTNGP11.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="RequestID" runat="server"
Font-Size="X-Small">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="checkboxSelect"
Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

</asp:DataGrid>
private void Button1_Click(object sender, System.EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach(DataGridItem DGridItem in DataGrid1.Items)
{

CheckBox myCheckbox =
(CheckBox)DGridItem.FindControl("checkboxSelect");

if(myCheckbox.Checked == true)
{
sb.Append(DataGrid1.DataKeys[DGridItem.ItemIndex].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
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...
4
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')"...
2
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...
2
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...
2
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...
2
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...
8
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...
0
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...
16
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...
10
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) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.