user controls: design-time vs. programmatic behavior

  • Thread starter Thread starter matt.delvecchio
  • Start date Start date
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
 
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
 
Back
Top