I'm a beginner in ASP.NET, just a question on user control events and page events, lets say I have a user control called myControl(.ascx) and a webform page my Page:
public partial class myPage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
...
}
}
public partial class myControl: System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
...
}
}
so my question is, which Load event happen first? my control's Load or myPage'Load? My textbook says it will be a random, undetermined order, but why we can't have a standard way like: all control events get raised first, then the postback event, isn't that more sensible?
Related
How can I add code to the Page_Load() method for every instance of System.Web.UI.Page without repeating the code?
I want, everytime a page loads, for the code
Debug.WriteLine("hello");
// other stuff here
// lots of lines of code
to run.
so I have many web pages for example
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// don't want to have to repeat all that code above here
// stuff that WebForm1 does
}
}
How can I run the first snippet for each Page_Load event without repeating the code for every webform?
MasterPage.cs
-----------------
protected void Page_Load(object sender, EventArgs e)
{
Debug.WriteLine("hello");
}
I want to execute a function on Page_load event of every System.Web.UI.Page from which derives my own CustomPage class (which obviously inherits from Page class as well)
what I have done so far it that I created CustomPage class like this:
public class CustomPage : System.Web.UI.Page
{
protected virtual void Page_Load(object sender, EventArgs e)
{
CallTOTheDesiredFunction(); //this is the call to the function I want
}
}
And in the derived Page classes I am doing this:
public class DerivedPage : CustomPage
{
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
//the rest of the page load event which executes from here on
}
}
As it is obvious, this approach is working but it is not the best solution since I have to call base.Page_Load(sender, e) on every derived page.
Is there a better solution to what I am trying to achieve?
Thank you in advance
Yes. It is better to override the Onload method rather than relying on deriving classes to call the base method.
You can still hook on the Load event in every page, but use the method in the base class.
public class CustomPage : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
CallTOTheDesiredFunction(); //this is the call to the function I want
base.OnLoad(e);
}
}
In UserControl1 there is a custom event which I want to wire in UserControl2.
in UserControl1 I have declared the custom event as:
public event MYDelegate SendMessage;
while my delegate defination is in other class library as:
public delegate string MYDelegate(string message);
I am firing SendMessage in my code as below:
SendMessage(txt.Text);
Kindly guide me how to wire SendMessage() event in UserControl2. My idea was do something like in below example but not sure how to get/ access UserControl1 object in UserControl2.
Please help me.
UserControl1.SendMessge+=ListnerMetod();
You are almost there. You just need to attach SendMessage to UserControl2's ListnerMetod.
As Mark Hall said, it is not a good practice to fire an event from one control to another without parent page knowing.
Here is the sample code of firing an event through a parent page.
Default.aspx (Parent Page)
<%# Register Src="SenderUserControl.ascx" TagName="SenderUserControl"
TagPrefix="uc1" %>
<%# Register Src="ReceiverUserControl.ascx" TagName="ReceiverUserControl"
TagPrefix="uc2" %>
<uc1:SenderUserControl ID="SenderUserControl1" runat="server" />
<uc2:ReceiverUserControl ID="ReceiverUserControl1" runat="server" />
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
SenderUserControl1.SendMessage += m => ReceiverUserControl1.ListnerMethod(m);
}
}
SenderUserControl.ascx
public delegate void MessageHandler(string message);
public partial class SenderUserControl : System.Web.UI.UserControl
{
public event MessageHandler SendMessage;
protected void Button1_Click(object sender, EventArgs e)
{
SendMessage("test");
}
}
ReceiverUserControl.ascx
public partial class ReceiverUserControl : System.Web.UI.UserControl
{
public void ListnerMethod(string message)
{
}
}
Credit to Mark Hall
If both UserControls are hosted by the same parent, attach a handler in the parent to the UserControls event that you want to subscribe to then call a method in the second UserControl in the handler.
I have some WebForms, such as First.ascx.cs, Second.ascx.cs, Third.ascx.cs and so on!
Well, I'd like to call a function (let's say, startFunction()) at the PreInit stage, and another one (let's say, endFunction()) and the PreRender stage, for EACH context.
So:
startFunction();
... First.ascx.cs PageLoad execution...
endFunction();
startFunction();
... Second.ascx.cs PageLoad execution...
endFunction();
startFunction();
... Third.ascx.cs PageLoad execution...
endFunction();
without write the same start/end function and copy and paste for each context I need to control. Is there a good strategy with .NET (3.5) and WebForms?
Inheritance!
Create a basecontrol where you attach to those events and then derive from it.
MarkzzzClass .cs
public abstract class MarkzzzClass : System.Web.UI.UserControl
{
//do something
}
BaseControl.cs:
public abstract class BaseControl : MarkzzzClass
{
protected override void OnPreRender(EventArgs e)
{
EndFunction();
base.OnPreRender(e);
}
protected override void OnInit(EventArgs e)
{
StartFunction();
base.OnInit(e);
}
}
First.ascx.cs:
public partial class First : BaseControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
I want to set the masterpage dynamically, But currently I have a base page with Page_PreInit that initializes/performs varioustasks needed for memberpages.
Now I know you could override the base page by putting (new protected void Page_OnInit(..)) in the member page but I don't want to as said the base page has got its job to do.
"this.MasterPageFile ="
Is there any way to set the masterpage after or before the Page_PreInit?
Or utilize both Page PreInt and BasePage PreInt ?
Thanks
EDIT: Rereading your question, I see your problem:
public class BasePage : WebPage {
protected void Page_PreInit(object sender, EventArgs e){
//do stuff here
}
}
public class MyPage : BasePage {
protected void Page_PreInit(object sender, EventArgs e){
//overwrites base class functionality
//Pretty sure you can:
base.Page_PreInit(sender,e);
}
}
Original answer
http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx#sectionToggle1
According to MSDN, you can assign it during Page_PreInit.
Is there a reason to do it before or after Page_PreInit? I'm not entirely sure anything useful comes before pre-init in the page life-cycle anyways.
(source: microsoft.com)
There isn't an earlier hook, except the page constructor, if you could do it there. But you wouldn't have access to any of the page values yet.