I've seen code to handle MasterPage events in the content Page, but if I'm loading a UserControl dynamically into the Page, can I handle the event in the UserControl instead?
Basically I have a button on the MasterPage, when it's clicked I need to make the UserControl do something, such as display text or change a value in a form.
Failing that, is it possible to make an event from one dynamically loaded UserControl fire in another on the same page? I could then replace the button in the MasterPage with one in another UserControl
Thanks all.
This is very similar to this stackoverflow question.
The answer to that question should also apply to yours: your Page knows its master and knows its child controls. It can wire-up a handler on the child control to an event on the master page, so the child control can perform necessary functionality when the even occurs on the master page.
Related
Allright so I have a slight issue when I want to load some basic usercontrols which contain an UpdatePanel inside to another page which is an usercontrol.
The set up:
Whenever an user clicks on a button a pop-up shows up containing some basic info on a certain person and a tab which contains the companies he worked for. The amount of companies he/she works for can range from 1 to 4~, so I do a query then for each company I get I add a view to a multiview, this view contains multiple simple user controls (Textfields inside an updatepanel). Now whenever I go to the page I get this error:
Cannot unregister UpdatePanel with ID 'UpdatePanel2' since it was not registered with the ScriptManager. This might occur if the UpdatePanel was removed from the control tree and later added again, which is not supported. Parameter name: updatePanel
Now I know this is a common error caused by generating UpdatePanels dynamically which aren't registered with the ScriptMaster. I can't add a PreInit event handler to the page which adds the UserControls with the UpdatePanels since it's an UserControl itself.
My question is:
How can I get rid of this nasty error in a not so nasty way e.g. not a hardcoded sub routine which adds the UpdatePanel to the scriptmaster
You could define the functionality you want to add inside your user controls. I have done the same in the past, adding the following, inside of my user control.
override protected void OnInit(EventArgs e) {
// your code here
}
Add a ScriptManager on your MasterPage before the UpdatePanel. And your updatePanel is a bit confusing so might change it as well. :)
Please I need someone to help me with this issue:
When I select an item from an .ascx usercontrol 'DropDownList Box' added to a master page with PlaceHolder1.Controls.Add(LoadControl("//UserControls/mycontrol.ascx"));
The .ascx user control disappears after the item has been selected
You don't show your code, so it's hard to provide an exact answer. but probably you add your user control dynamically only one time and it disappears after each postback.
You need to load Usercontrol after each postback so you need to load Usercontrol in something like Page_load method if you don't.
I have two user controls on a page - UC1,UC2. Each usercontrol have a asp:button. Now if I click the any of the buttons the page post back. Now if I click the button of UC1 then the following happen.
Viewstate load for two user controls.
Page load called for two user controls.
The event handler of UC1 button is fired.
Now if user click on the UC1 button, I do not want to load the view state of UC2 user controls at server because the viewstate of UC2 is large.
The execution of UC1 is not dependent of UC2.
Please suggest how can I achive this.
Thanks!
What it sounds like your asking for is the ability to do partial postbacks. That is postbacks that only affect a portion of the page. There are a number of ways to accomplish this such as using the Microsoft provided AJAX controls (http://www.asp.net/ajax) or rolling your own with JQuery and web methods (this method however is stateless and should be expected as such). Specifically you will want to look at UpdatePanels and PostBack Triggers.
Hope this helps.
I have asp.net application and a TabContainer from AjaxControlToolkit on a form. There are 4 tabs, each containing UpdatePanel, hidden button and some custom .ascx (each with it's own javascript file). Buttons are triggers for panels to update the contents and they are triggered from the 'OnClientActiveTabChanged' event of the TabContainer.
This technique is described here and similiar here. It's pretty simple when looking at it.
The only problem I have is that the whole scenario works when used as a separate page but it doesn't seem to work when masterpage is around that page. Suddenly buttons act as full postback controls.
Do you have any idea what's the reason?
Assuming the buttons your referring to are on the master page, I think you'll want to register the master page buttons as update panel triggers.
http://www.asp.net/ajax/tutorials/understanding-asp-net-ajax-updatepanel-triggers
I have a UserControl inheriting from another, and my question is simple. Does the control's page load fire first, or does the base class page load fire first?
"The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded."
From this link: http://msdn.microsoft.com/en-us/library/ms178472.aspx
So to answer plainly, the Page_Load event is called before the load event in user controls
If you are talking about an actual inheritance, and not control composition, then all the standard Object Oriented rules apply.
Because these are not separate object, there is only one Load event on your control, and there can be only one Page_Load method, unless you explicitly hide it using the new modifier. So, in essence, there is no difference between the Child page load, and the Parent page load... they are one in the same.
The control's Page_Load should fire first, I believe. Other than the Page_Init event, all other initiation events occur up the control hierarchy.
Edit: I'm wrong up there. The page fires its load event then recursively calls it on child controls, which recursively call it on its child controls, and so on. My bad...