0
votes

I have usercontrol in my page loaded using Page.Controls.Add(ctrl)

Now, I have button in that user control and want to fire one function when user clicks on that user control's button.

I have that user control on lots of pages and want to fire different function on that button click of user control.

1
C# or VB.NET? Also, do you really need to add it dynamically or can you add it declaratively to the page?Tim Schmelter
C#, yes. I added controls like I said above. UserControl uc = new UserControl(); uc = (UserControl)Page.LoadControl("WebUserControl1.ascx"); dvctrl.Controls.Add(uc);k-s
But you haven't answered why you think that it must be added dynamically via LoadControl since this approach is much more difficult and error-prone(lifecycle issues) than adding it declaratively on the aspx markup. For instance you should add it in Page_Init rather than Page_Load.Tim Schmelter

1 Answers

1
votes

In general you should communicate from your UserControls to the Pages via Events. So declare a custom event in the UserControl that'll be raised in the button-click handler and can be handled by the page. On this way every page using this control can react differently.

http://www.codeproject.com/Articles/8797/Mastering-Page-UserControl-Communication#4.3