Instead of page_load i want to write PageName_Load, how can i do
Recently this question was asked my the one of the company in interview
REgards Praveen
Instead of page_load i want to write PageName_Load, how can i do
Recently this question was asked my the one of the company in interview
REgards Praveen
Attaching an event handler to it in the constructor:
public class MyPage : System.Web.UI.Page
{
public MyPage()
{
this.Load += new EventHandler(MyPage_Load);
}
void MyPage_Load(object sender, EventArgs e)
{
}
}
I don't believe that there is any support in changing the default convention; could be wrong.
It was well described long time ago here at StackOverflow: What calls Page_Load and how does it do it?
To supplement Brian and invisible's post, you can also change your web templates to create a page the way you want.
Navigate to your ASP templates directory:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\1033
This may change depending on your environment, but that should get you in the neighborhood).
Locate and extract WebForm.zip
(Always keep a backup!). You'll note a few files in this template:
Default.aspx
Default.aspx.cs
Default.aspx.designer.cs
WebForm.vstemplate
Metadata that describes the template required libraries, etc.From there, a few edits make it it the default behavior:
AutoEventWireup="true"
from the heading.Page_Load
to PageName_Load
(or you could use Page$classname$_Load
i suppose...)this.Load += new EventHandler(PageName_Load)
Save all changes, re-compress back to the zip file and make sure it's in your template directory. Now all new pages will use that template.
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
var myPageLoadDelegate = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this, this.GetType().BaseType.Name + "_Load", true, false);
if (myPageLoadDelegate != null)
{
this.Load += myPageLoadDelegate;
}
}
}
public partial class WebForm1 : BasePage
{
protected void WebForm1_Load(object sender, EventArgs e)
{
}
}