dynamic button
-
hi, in my page load iam creating a row consisting of text box,drp down and button. on clicking the button a similar row should be created. how to fire the event of dynamically created button? code is there but not working. plz help me out Regards, Boon
-
hi, in my page load iam creating a row consisting of text box,drp down and button. on clicking the button a similar row should be created. how to fire the event of dynamically created button? code is there but not working. plz help me out Regards, Boon
Hi there. Are you assigning the event handler function to your button's click event?
myButton.Click += new System.EventHandler(myButtonClickHandlerFunc);
Here's a fuller example of the kind of thing you're describing. Does this help?<% @Page Language="C#" %>
<script runat="server">
const string SESSION_ROWCOUNT = "RowCount";
void Page_Load(object o, EventArgs e)
{
if (!IsPostBack)
// create the first row
CreateARow(0);
else
// recreate all the rows that existed
RecreateRows();
}void CreateARow(int index)
{
TableRow r = new TableRow();
TableCell c = new TableCell();TextBox tb = new TextBox(); tb.ID = "text\_" + index.ToString(); c.Controls.Add(tb); Button b = new Button(); b.ID = "button\_" + index.ToString(); b.Text = "Add Another"; b.Click += new System.EventHandler(ButtonClick); c.Controls.Add(b); r.Cells.Add(c); myTable.Rows.Add(r); // store the count in a session variable so we can recreate // the existing rows on a postback Session\[SESSION\_ROWCOUNT\] = myTable.Rows.Count;
}
void ButtonClick(object o, EventArgs e)
{
CreateARow(myTable.Rows.Count);
}void RecreateRows()
{
int iCount = 0;
if (Session[SESSION_ROWCOUNT] != null)
iCount = Convert.ToInt32(Session[SESSION_ROWCOUNT]);for (int i=0; i<iCount; i++) { CreateARow(i); }
}
</script>
<html>
<head>
<title>Dynamic TableRow Example</title>
</head><body>
<form runat="server">
<asp:Table id="myTable" runat="server">
</asp:Table><br /> <br /> <asp:Button id="btnForcePostback" runat="server" Text="Force a non-row-creating postback" /> </form>
</body>
</html>