Wiring up dynamic control

B

brett

I load a dynamic control once a button is clicked. In the control is
another button. However, I can't get its click event to fire. Here's
the pattern:

Page1.aspx
---------
SomeButton_Click()
{
Control control = LoadControl("mycontrol.ascx");
control.ID = "thecontrolID";
panel1.Controls.Add(control);
}


mycontrol.ascx
----------------------
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
this.mybutton.Click += new
mybuttonClickEventHandler(btnmybutton_Click);
}


Once SomeButton is clicked, the user control loads via AJAX and
appears. I see btnmybutton from the user control. Once I click it,
the click event doesn't fire. Any ideas?

Thanks.
 
B

bruce barker

you are not hooking up the event handler on postback, so there is no way
for it to fire.

-- bruce (sqlwork.com)
 
B

brett

You've probably forgotten to set it's ID property...

Not quite. I creat the control on button_click(). That initiates a
postback. The control must be created again after postback. That
leaves room to make a mistake in assigning the same ID, which is where
I went wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top