473,467 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Parsing checkboxes in ASP.NET that are created dynamically

Paul Johnson
97 New Member
Hi,

I have a very simple page which uses jqueryui to create a couple of tabs.

Tab1 is for data entry. Really simple, a couple of text boxes, a dropdown list and a few buttons. It performs a sort on the data entered and bungs it into an SQL table.

Tab2 generates a table in the format

checkbox id sortdir data timetaken

This is generated dynamically like this

Expand|Select|Wrap|Line Numbers
  1. private void generateTable()
  2.         {
  3.             long timer = 0;
  4.             int sort = 0;
  5.             string data = "";
  6.             SqlConnection sql = connect();
  7.             SqlDataReader sqlr = null;
  8.  
  9.             for (int n = 1; n < getRows() + 1; ++n)
  10.             {
  11.                 TableRow tRow = new TableRow();
  12.                 Table1.Rows.Add(tRow);
  13.                 using (SqlCommand sqlc = sql.CreateCommand())
  14.                 {
  15.                     sqlc.CommandType = CommandType.StoredProcedure;
  16.                     sqlc.Parameters.AddWithValue("@ID", n);
  17.                     sqlc.CommandText = "spReadData";
  18.                     sql.Open();
  19.                     sqlr = sqlc.ExecuteReader();
  20.                     if (sqlr.Read())
  21.                     {
  22.                         timer = (long)sqlr["timetaken"];
  23.                         sort = (int)sqlr["sortedby"];
  24.                         data = (string)sqlr["sortedvalues"];
  25.                     }
  26.                     TableCell check = new TableCell();
  27.                     tRow.Cells.Add(check);
  28.                     System.Web.UI.WebControls.CheckBox cb = new CheckBox();
  29.                     cb.ID = n.ToString();
  30.                     check.Controls.Add(cb);
  31.  
  32.                     for (int g = 0; g < 4; g++)
  33.                     {
  34.                         TableCell cell = new TableCell();
  35.                         tRow.Cells.Add(cell);
  36.                         System.Web.UI.WebControls.Label lbl = new Label();
  37.                         switch (g)
  38.                         {
  39.                             case 0:
  40.                                 lbl.Text = n.ToString();
  41.                                 break;
  42.                             case 1:
  43.                                 lbl.Text = timer.ToString();
  44.                                 break;
  45.                             case 2:
  46.                                 lbl.Text = sort == 0 ? "Ascending" : "Decending";
  47.                                 break;
  48.                             case 3:
  49.                                 lbl.Text = data;
  50.                                 break;
  51.                         }
  52.                         cell.Controls.Add(lbl);
  53.                     }
  54.                     sql.Close();
  55.                 }
  56.             }
  57.         }
  58.  
As you can see, the code generates an ID for the checkbox which is always going to be the same.

Problem is this. The checkboxes are to be used to select if the output is to be put into an XML file for export. I could add an event to each checkbox to store/remove from a list but I'd rather not.

The code I have to parse the checkboxes though isn't a happy beast - it's simply not picking them up (checked using the debugger in VS2010)

Expand|Select|Wrap|Line Numbers
  1. private void exportXML(object s, EventArgs e)
  2.         {
  3.             List<int> ids = new List<int>();
  4.             long tt;
  5.             int dir;
  6.             string data;
  7.             int n = 0;
  8.             foreach (Control ctrl in this.Controls)
  9.             {
  10.                 if (ctrl is CheckBox)
  11.                 {
  12.                     if (((CheckBox)ctrl).Checked == true)
  13.                         ids.Insert(n++, Convert.ToInt32(((CheckBox)ctrl).ID));
  14.                 }
  15.             }
  16.         }
  17.  
A debug point at
Expand|Select|Wrap|Line Numbers
  1. if (ctrl is CheckBox)
shows that the code is working at this point, but is not showing anything that was generated dynamically.

The event is only fired off on the tab with the dynamic table on it.

Any clues on why this is failing?

Paul
May 16 '11 #1
1 1629
Paul Johnson
97 New Member
Quick update

Changing
Expand|Select|Wrap|Line Numbers
  1. foreach (Control ctrl in this.Controls)
to
Expand|Select|Wrap|Line Numbers
  1. foreach (Control ctrl in Table1.Controls)
is getting me closer, but it's still not snagging the control box, only a TableCellControlCollection

Erg!
May 16 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: fumihiko | last post by:
Hi, I created an activex control (C++), and it uses another COM dll (C++). This COM dll links with a static library that dose some calculation. (both are debug multithreaded dll) I created a...
3
by: Forconi Boris via .NET 247 | last post by:
Hi, I'm working on a project in witch I have to list data(products that can be selected with a certain quantity) from anXML document, with key words entered by the user. A Table isdynamically created...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
0
by: Jose Walker | last post by:
Hi there! I've been fighting with an issue that by now is driving me crazy. I've developed a ASP.NET 2 web part from which I build dynamically an ASP.NET 2 Menu control, I populate the control...
6
by: Ros_Au | last post by:
I create a table dynamically using VB, How do I get HTML code for that table into a string variable ??? any sugestion, Thanks!!!
2
by: jmarendo | last post by:
Hello, After reading through the "Table Basics - DOM - Refer to table cells" example at mredkj.com , I modified the code for my own purposes. In the modified version, I create a hyperlink and...
7
by: Lou | last post by:
I have a menu item that creates a new command button. Each time I create a new command button I need to position it to the left of the last created button. In VB6 I could create a control array,...
4
by: imranabdulaziz | last post by:
Dear All, I am using asp.net2.0, C#, sql2005 using Visual studio 2005 Let Me explain the scenario I have checkboxlist containg 15 field. Based on no of checked field . I created...
2
by: Sebarry | last post by:
Hi, I have a Javascript function that creates a number of checkboxes dynamically using document.createElement. The checkboxes are created fine but even when they are checked they don't appear in...
0
by: Venkata Krishnam Raju | last post by:
Unable to find the Textbox in Gridview which Template controls created dynamically
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...
1
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.