473,847 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically Adding Controls

72 New Member
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.    {
  4.        // Get the DataTable from Session  
  5.        DataTable dt = (DataTable)Session["MyDt"];
  10.        // Loop through the rows  
  11.        foreach (DataRow dr in dt.Rows)  
  12.        {
  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();
  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");
  31.            removebutton.Click += new EventHandler(this.removebutton_Click);
  33.            TableCell tc4 = new TableCell();
  34.            tc4.Controls.Add(removebutton);
  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); 
  43.            // Lastly add the row to the table  
  44.            tblFilter.Controls.Add(tr);
  46.        }
  48.    }  
Jun 8 '10 #1
3 2113
72 New Member
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.Invalid OperationExcept ion}. 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);
  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;
  14.         DataTable dt = (DataTable)Session["MyDt"];
  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();
  26.     }
Jun 8 '10 #2
72 New Member
Scrapped the whole idea and used a Gridview with a Datatable to filter dynamically.
Jun 9 '10 #3
9,735 Recognized Expert Moderator Expert
:) I'm glad you solved your problem :)

Jun 10 '10 #4

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

Similar topics

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 newButton = new Button(); this.Controls.Add(newButton); newButton.Parent = tabPageSummary; newButton.BringToFront();
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 save, which are all dynamically created in the code behind and have event handlers registered. In the page life cycle, event handlers are called after the page is loaded. The problem is, the save is done after the controls are loaded, so the...
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 by desktopdefault.aspx. The buttons were used to "navigate" withing the same page. I had the problem with "fail to load viewstate ...." due to a mismatch controls in the treeview in the viewstate. Dynamically removing controls was messy and it...
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. This is working fine, and I can add a control (TextBox or DropDownList) dynamically for the user to fill in a value. This works well for just adding controls for data entry. Now, I want to add validators on the fly as well and it not going quite...
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 anywhere. I have managed to add Labels using the following code: Dim extralabel As Label = New Label extralabel.Text = "Generated Label" Me.Controls.Add(extralabel)
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. The article that it loads from the Database is a xml field that contains html code for the article. I added some custom html to the database article called <question id="22" /> When the article contains inline questions are parse out the start...
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 following error when the DDL is added (but not the Label): "Control 'ctl07' of type 'DropDownList' must be placed inside a form tag
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 and add them to a placeholder no problem. However when I post back and the page displays again then all of the controls are gone. I create them when the IsPostBack is false, because the idea is to validate on post back, and if the data is invalid...
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 selected, the row is added using JavaScript. The script uses cloneNode to clone a hidden template row and all of its children and then adds the new row to the table, updates all of the child control Ids and sets visibility etc. The hidden...
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 97/2000. So I guess my questions are: First up, a double check - You can't dynamically add controls to access 97/2000 forms right?
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...
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...
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...
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...
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...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.