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

Dynamically Adding Controls

So i'm making my own filter page for a gridview and have ran into some problems. When the user clicks on the add filter button, I create a row in an ASP table displaying the filter. The filtering works correctly and adds the row to the table. But att the last cell is a button that is created when the row is added to delete the filter. For some reason the delete button will not fire the event ive created for it. Any suggestions? Code that creates the tablerow and button.
Expand|Select|Wrap|Line Numbers
  1. private void BindDtToTable()  
  2.    {
  3.  
  4.        // Get the DataTable from Session  
  5.        DataTable dt = (DataTable)Session["MyDt"];
  6.  
  7.  
  8.  
  9.  
  10.        // Loop through the rows  
  11.        foreach (DataRow dr in dt.Rows)  
  12.        {
  13.  
  14.            // Create a table row  
  15.            TableRow tr = new TableRow();
  16.            tr.ID = dr["id"].ToString();
  17.            // Create table cells  
  18.            TableCell tc1 = new TableCell();
  19.            tc1.Text = dr["Column1"].ToString();
  20.            TableCell tc2 = new TableCell();
  21.            tc2.Text = dr["Column2"].ToString();
  22.            TableCell tc3 = new TableCell();
  23.            tc3.Text = dr["Column3"].ToString();
  24.  
  25.            Button removebutton = new Button();
  26.            removebutton.Text = "Remove";
  27.            removebutton.ID = "btnRemove" + dr["id"].ToString();
  28.            removebutton.EnableViewState = true;
  29.            removebutton.Attributes.Add("runat", "server");
  30.  
  31.            removebutton.Click += new EventHandler(this.removebutton_Click);
  32.  
  33.            TableCell tc4 = new TableCell();
  34.            tc4.Controls.Add(removebutton);
  35.  
  36.  
  37.            // Add the columns to the table row  
  38.            tr.Controls.Add(tc1);  
  39.            tr.Controls.Add(tc2);  
  40.            tr.Controls.Add(tc3);
  41.            tr.Controls.Add(tc4); 
  42.  
  43.            // Lastly add the row to the table  
  44.            tblFilter.Controls.Add(tr);
  45.  
  46.        }
  47.  
  48.    }  
  49.  
Jun 8 '10 #1
3 2087
Ok so I have this event handler bound to my remove button. Everytime i click on the remove button i get. Collection was modified; enumeration operation might not execute."System.Exception {System.InvalidOperationException}. Here is my remove operation.
Expand|Select|Wrap|Line Numbers
  1. protected void removebutton_Click(object sender, EventArgs e)
  2.     {
  3.         string script = "alert('i made it to the remove button');";
  4.         ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "testing", script, true);
  5.  
  6.         string temp = ((Button)sender).ID;
  7.         string id = temp.Substring(9);
  8.         int idnum = Convert.ToInt32(id);
  9.         string expression = Convert.ToString(Session[idnum]);
  10.         string filterexpression = dsHistory.FilterExpression;
  11.         string newexpression = filterexpression.Replace(expression, "");
  12.         dsHistory.FilterExpression = newexpression;
  13.  
  14.         DataTable dt = (DataTable)Session["MyDt"];
  15.  
  16.         //loop through table and find cell with id and remove
  17.         foreach (DataRow dr in dt.Rows)
  18.         {
  19.             if (dr["id"].ToString() == id )
  20.             {
  21.                 dr.Delete();
  22.             }
  23.         }
  24.         BindDtToTable();
  25.  
  26.     }
  27.  
Jun 8 '10 #2
Scrapped the whole idea and used a Gridview with a Datatable to filter dynamically.
Jun 9 '10 #3
Frinavale
9,735 Expert Mod 8TB
:) I'm glad you solved your problem :)

-Frinny
Jun 10 '10 #4

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

Similar topics

1
by: Robert W. | last post by:
I'm trying to dynamically add some controls onto a Tab page and am encountering a strange phenomenon that I'd like to get some feedback on. Here's the guts of the code that I'm using: Button...
1
by: Greg | last post by:
In my ASP.NET application I have a button that when pressed, data needs to be saved based on what the user typed in. I have other controls on the same page that should be updated as a result of the...
0
by: Sinisa Ruzin | last post by:
Hi all, I had problem with dynamically adding/removing controls;ascx, Controls.Add(Page.LoadControl... in the same page of the IBuySpy portal. ASP.NET, C#. I added buttons to the main ASCX loaded...
4
by: MattB | last post by:
Hello. I'm creating a page to edit a person's record (name address, etc) using a DataGrid. I want the fields that can be edited to be created at runtime based on a list of columns in web.config....
6
by: Nathan Sokalski | last post by:
I am trying to dynamically add controls to my page, but am having trouble with controls such as buttons. I have been able to add simple controls such as Label controls, because they can be placed...
3
by: Ben Dewey | last post by:
Okay, so I have a base Page class called ArticlesPageBase. This base class has reads in an overridable ArticleId and Loads the data into a Panel object in the ContentPlaceHolder of the Master. ...
3
by: Mark Denardo | last post by:
I'm trying to dynamically create and add controls to a web page: Label obj1 = new Label(); DropDownList obj2 = new DropDownList(); Controls.Add(obj1); Controls.Add(obj2); But I get the...
2
by: Chris Botha | last post by:
I am adding controls dynamically to the page as per the samples there are on the Web and it works - I can create text boxes, buttons, my own user controls, whatever, on the fly and initialize them...
4
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
3
by: Mat | last post by:
Hi all, It's been a long while since I've posted or used access and one of the things that had me stop was the realisation I couldn't dynamically add new controls to a form. I am using Access...
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: 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
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...
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
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,...
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
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...

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.