Adding EventHandler Dynamically...

Results 1 to 2 of 2

Thread: Adding EventHandler Dynamically...

  1. #1
    Join Date
    Dec 1969

    Default Adding EventHandler Dynamically...

    &nbsp;<BR>I am trying to create controls (dropdown, text and button) on the fly and placing them in the table cells. At design time, I have only on erow with Dropdown, Text and a button control. Onclick of this button, I am trying to add a row to the table, and place one more set of controls in this newly created row. So, the new row has a button to which I have to assign an event handler, so that on cick of this, one more row with controls can be added to table.<BR><BR>Now, I have attached an eventhandler to my button control created and placed on a new table row, dynamically. But the on-click of this button is not firing. It just re-loads the page, there by removing the 2nd row that was dynamically created and the table now will have only one row ( as it was in the design mode)<BR><BR>Here is my code :<BR>public void AddRows(object sender, System.EventArgs e )<BR>{<BR>TableRow r=new TableRow();<BR>TableCell c;<BR>c=new TableCell();<BR>System.Web.UI.WebControls.Button btn = new System.Web.UI.WebControls.Button();<BR>btn.Text = "+" ;<BR>c.Controls.Add(btn);<BR>r.Cells.Add(c);<BR>tb l_Criteria.Rows.Add(r);<BR>btn.Click += new System.EventHandler(Btn_Click);<BR>}<BR><BR>public void Btn_Click(Object sender, EventArgs e) <BR>{<BR>Response.Write("You clicked the button.");<BR>}<BR><BR>Where am I going wrong ? <BR> <BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Adding EventHandler Dynamically...

    Your problem isn&#039;t in adding the event handler... The ViewState isn&#039;t storing the information for your table since the controls (the new row, button, etc.) are being added dynamically. I don&#039;t know wnough about your specific problem to suggest an alternate solution. I suppose you could add a bunch of rows on page load, and set the visibility to false. The onclick event can just set the visibility of the next row to true and increment a counter you have in viewstate:<BR><BR>tbl_Criteria.Rows[ViewState("VisibleRows")].Visible = True;<BR>ViewState("VisibleRows") += 1;<BR><BR>You&#039;ll have to check the C# syntax... I am a VB.NET developer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts