18 September 2010

Meaning For Dotnet First line of Code

Meaning For Dotnet First line of Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
@ Page directive on top of page
Page Language="C#" Denotes C# language is used in this project

AutoEventWireup attribute defaults to true,

ASP.NET does not require that you explicitly bind event handlers to a page event such as Load.

When AutoEventWireup is false, you must explicitly bind the event to a method. For example, if you have a Page_Load method in the code for a page, the method will be called in response to the Load event only if you write code like that in the following example

Otherwise you have to bind the event explicitly

public partial class AutoEventWireupExample : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
Response.Write("Executing Page_Load");
}
override protected void OnInit(EventArgs e)
{
this.Load += new System.EventHandler(this.Page_Load);
}
}

The following code example shows how to set or read the AutoEventWireup property in code.
// Get the current AutoEventWireup property value.
Console.WriteLine(
"Current AutoEventWireup value: '{0}'",
pagesSection.AutoEventWireup);

// Set the AutoEventWireup property to false.
pagesSection.AutoEventWireup = false;


The following example shows the two forms of method signatures that are automatically attached to page events when AutoEventWireup is true.

protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello world");

}
// This method will be automatically bound to the Load event
// when AutoEventWireup is true only if no overload having
// object and EventArgs parameters is found.
protected void Page_Load()
{
Response.Write("Hello world");
}



// Following are three alternative ways of binding an event
// handler to an event when AutoEventWireup is false. For
// any given event do this binding only once or the handler
// will be called multiple times.

// You can wire up events in the page's constructor.
public _Default()
{
Load += new EventHandler(Page_Load);
}

// You can override the OnInit event and wire up events there.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Load += new EventHandler(Page_Load);
}

// Or you can override the event's OnEventname method and
// call your handler from there. You can also put the code
// execute when the event fires within the override method itself.
protected override void OnLoad(EventArgs e)
{
Page_Load(null, null);
base.OnLoad(e);
}

protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello world");
}

Inherits="_Default"

the Inherits attribute tells the runtime the name of the class it will use as a base class for this web form,

look at the CodeFile (code-behind) file for the ASPX page.

using System;
using System.Web.UI;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
}
‘partial’ keyword.
Partial allows us to split a class definition across two or more source code files.
The partial keyword plays an important role because it will allow the runtime to extend the definition of our _Default class with additional members.

No comments:

Post a Comment

Comments Welcome

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...