Dec
20
Written by:
Wil Dobson
12/20/2007 12:29 PM
In the event that you need to setup click events for your dynamically created buttons, I would suggest you use button Commands and CommandArgs to indicate what to do and what to do it to.
In this example, I placed a PlaceHolder control into the form where I will want to dynamically place buttons.
// add a dynamic button with command args
Button mybtn = new Button();
mybtn.ID = "btnDynamicButton";
mybtn.Text = "Edit";
mybtn.CommandName = "Edit";
mybtn.CommandArgument = "Comments";
mybtn.Command += new CommandEventHandler(mybtn_Click);
PlaceHolder1.Controls.Add(mybtn);
Now here is the event handler (will handle all dynamic buttons created with the above code as indicated by the mybtn.Command += ...)
protected void mybtn_Click(object sender, EventArgs e)
{
// figure out which action is being requested
if (((Button)sender).CommandName == "Edit")
{
// we are editing
// figure out what area is being worked on
string AreaToEdit = ((Button)sender).CommandArgument;
// now we know that we are editing the comments content
// put here whatever you want to do to open an editor
// just for testing
Response.Write("Mode: " + ((Button)sender).CommandName + " - Action: " + AreaToEdit);
}
}
Copyright ©2007 Wil Dobson
Tags: