Dynamically Adding Controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • benwizzle
    New Member
    • May 2010
    • 72

    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.
    Code:
    private void BindDtToTable()  
       {
           
           // Get the DataTable from Session  
           DataTable dt = (DataTable)Session["MyDt"];
    
           
    
    
           // Loop through the rows  
           foreach (DataRow dr in dt.Rows)  
           {
               
               // Create a table row  
               TableRow tr = new TableRow();
               tr.ID = dr["id"].ToString();
               // Create table cells  
               TableCell tc1 = new TableCell();
               tc1.Text = dr["Column1"].ToString();
               TableCell tc2 = new TableCell();
               tc2.Text = dr["Column2"].ToString();
               TableCell tc3 = new TableCell();
               tc3.Text = dr["Column3"].ToString();
    
               Button removebutton = new Button();
               removebutton.Text = "Remove";
               removebutton.ID = "btnRemove" + dr["id"].ToString();
               removebutton.EnableViewState = true;
               removebutton.Attributes.Add("runat", "server");
    
               removebutton.Click += new EventHandler(this.removebutton_Click);
               
               TableCell tc4 = new TableCell();
               tc4.Controls.Add(removebutton);
               
    
               // Add the columns to the table row  
               tr.Controls.Add(tc1);  
               tr.Controls.Add(tc2);  
               tr.Controls.Add(tc3);
               tr.Controls.Add(tc4); 
    
               // Lastly add the row to the table  
               tblFilter.Controls.Add(tr);
              
           }
           
       }
  • benwizzle
    New Member
    • May 2010
    • 72

    #2
    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.
    Code:
    protected void removebutton_Click(object sender, EventArgs e)
        {
            string script = "alert('i made it to the remove button');";
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "testing", script, true);
    
            string temp = ((Button)sender).ID;
            string id = temp.Substring(9);
            int idnum = Convert.ToInt32(id);
            string expression = Convert.ToString(Session[idnum]);
            string filterexpression = dsHistory.FilterExpression;
            string newexpression = filterexpression.Replace(expression, "");
            dsHistory.FilterExpression = newexpression;
    
            DataTable dt = (DataTable)Session["MyDt"];
    
            //loop through table and find cell with id and remove
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["id"].ToString() == id )
                {
                    dr.Delete();
                }
            }
            BindDtToTable();
    
        }

    Comment

    • benwizzle
      New Member
      • May 2010
      • 72

      #3
      Scrapped the whole idea and used a Gridview with a Datatable to filter dynamically.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        :) I'm glad you solved your problem :)

        -Frinny

        Comment

        Working...