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");
}
Related
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?
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);
}
}
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.
I have two user controls inside an ASPX page. All I want to do is pass one single int from one control to the other during page load.
I have tried several of the examples on here but none of them work - sorry I mean I can't get them to work! Here's what I've got:
Default.ASPX
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ucBottom.varBottomID = ucTop.varBottomID;
}
}
UCTOP
public partial class ucTop : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
int varBottomID = 100;
}
}
UCBOTTOM
public partial class ucBottom : System.Web.UI.UserControl
{
public int varBottomID { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
BottomText.Text = Convert.ToUInt32(varBottomID).ToString();
}
}
There are no errors except for BottomText.Text returning 0 instead of 100. Also int varBottomID = 100; is stating that it is assigned but never used (ucTop).
From one of the answers here it's good practice for one UC not to be aware of the other and to use the parent to pass data. But alas not for me.
As always, any help would be appreciated (but in a below dummies guide format!).
EDIT - Sorry A typo crept in the code in Default...
In your main page, it's not really changing anything -- it's setting ucBottom.varBottomID to itself. (Edit: appears to have been fixed)
In ucTop, int varBottomID = 100; does nothing -- it sets a variable inside the function, which won't be visible outside.
The result of those two issues is that you never end up having anything communicated from ucTop to ucBottom.
(BTW, convention is to start class names with an uppercase letter, and instance names with a lowercase letter. It's not exactly obvious from looking at the code whether you're attempting to access static or instance members of your classes. From here on i'm assuming you have a ucBottom control, which is an instance of the ucBottom class, and the same for ucTop. It's the only case i see where your code, as pasted, would compile without errors.)
You may need something similar to:
public partial class _Default : Page
{
protected void Page_Load(sender obj, EventArgs e)
{
ucBottom.varBottomID = ucTop.varBottomID; // fix the first issue
}
}
public partial class ucTop : UserControl
{
public int varBottomID { get; set; } // fix the second issue
protected void Page_Load(object src, EventArgs e)
{
varBottomID = 100;
}
}
You will also need to pick different events to trigger on, if you go this route. If everything triggers on Load, you have a problem -- Load fires on the page first, then on the child controls. So the property won't get set correctly. (The setting goes in 3 steps: ucTop deciding the correct value to set, _Default setting it from top to bottom, and ucBottom using it. You need that sequence to go decide, set, use in order for it to work correctly, but if everything triggers on Load you'll end up with set, decide, use or set, use, decide.) You might consider letting _Default do its thing on LoadComplete, and ucBottom on PreRender.
Modify your code as needed.
public partial class ucTop : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
ucBottom.varBottomID = 100;
}
}
public partial class ucBottom : System.Web.UI.UserControl
{
public int varBottomID { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
BottomText.Text = Convert.ToUInt32(varBottomID).ToString();
}
}
Defining a class which both UserControls could access a static variable would work:
public static class RequiredVars
{
public static int BottomID { get; set;}
}