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.    {
  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 2113
benwizzle
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);
  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
benwizzle
72 New Member
Scrapped the whole idea and used a Gridview with a Datatable to filter dynamically.
Jun 9 '10 #3
Frinavale
9,735 Recognized Expert Moderator Expert
:) 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
6136
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();
1
1676
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...
0
1729
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...
4
3611
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...
6
436
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)
3
2201
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...
3
5499
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
2
359
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...
4
4494
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...
3
3688
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?
0
9887
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, 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...
0
10983
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
10648
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...
0
10341
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 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...
0
9485
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...
1
7883
isladogs
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...
1
4529
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
4119
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3164
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.