user controls: design-time vs. programmatic behavior

M

matt.delvecchio

hello,

ive got a usercontrol that has some textboxes, a button, and a handler
routine for its button_click. when this usercontrol is dropped into a
page via the designer, it functions as expected -- when the button is
clicked its event handler loads up the routine and i do db stuff.

however, i am now adding the control to the page programmatically (on a
page's button click), via:

Controls.PackageInfo info =
(Controls.PackageInfo)LoadControl("~/controls/packageInfo.ascx");
info.ID = "ucPackageInfo";
phInfoPanel.Controls.Add(info);

....and it shows up. but ive discovered that the control's button's
event handler no longer works -- setting a breakpoint on it shows that
its never called. the hanlder is mapped to it via:

private void InitializeComponent()
{
this.btnSubmit.Click += new
System.EventHandler(this.btnSubmit_Click);
}

....which the debugger hits.

any ideas why it no longer handles itself??


thanks!
matt
 
M

matt.delvecchio

ok, found the problem. i was only adding the control on the page's
button click event... so when the control's button was clicked, it was
firstly re-loading the page -- sans user control; thus no control event
hanlder could follow.

the solution was to load the user control in the page_load, all the
time (but invisible). on the page's button click event, make it
visible. then when the control's button is clicked, it will fire its
events/handlers (since it now exists!).


matt
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top