1
      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

Varma
  • 11
  • 1
  • I know this is just an interview question, but something to keep in mind: for the sake of code maintainability, actually doing this would be a bad idea. – mikemanne Jul 05 '12 at 17:56

4 Answers4

5

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.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Agreed, however I tend to like to make it explicitly known it's an event attachment. (Besides, [MSDN likes doing so, too](http://msdn.microsoft.com/en-us/library/system.web.ui.control.load.aspx)!) – Brad Christie Jul 05 '12 at 16:13
1

It was well described long time ago here at StackOverflow: What calls Page_Load and how does it do it?

Community
  • 1
  • 1
Sergei B.
  • 3,227
  • 19
  • 18
0

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
    ASPX page template
  • Default.aspx.cs
    Code-Behind template (where you would typically add your own code)
  • Default.aspx.designer.cs
    IDE-Generated file (which manages the list of controls on the page).
  • WebForm.vstemplate Metadata that describes the template required libraries, etc.

From there, a few edits make it it the default behavior:

  1. Edit Default.aspx and remove AutoEventWireup="true" from the heading.
  2. Edit Default.aspx.cs and change Page_Load to PageName_Load (or you could use Page$classname$_Load i suppose...)
  3. Edit Default.aspx.designer.cs and add a constructor that binds 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.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
0
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)
    {

    }
}
Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68