By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,612 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,612 IT Pros & Developers. It's quick & easy.

Dynamically Adding Controls

P: 72
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
Share this Question
Share on Google+
3 Replies


P: 72
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

P: 72
Scrapped the whole idea and used a Gridview with a Datatable to filter dynamically.
Jun 9 '10 #3

Frinavale
Expert Mod 5K+
P: 9,731
:) I'm glad you solved your problem :)

-Frinny
Jun 10 '10 #4

Post your reply

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