• No results found

When an ASP.NET page is loaded, a structured series of events is fired in a set order. You write code that responds to these events rather than interspersing it with HTML as the general practice with ASP. Figure 7-1 shows the ASP.NET event order.

As you can see, the first event to be fired when a page is loaded is the Page_Loadevent and the last to be fired is the Page_Unloadevent. The Page_Loadevent is fired every time a page is loaded. In between the Page_Loadand the Page_Unloadevents, control events are fired. A control event is an event that is wired to a control of some sort. ASP.NET provides many types of controls including HTML controls, Web controls, user controls, validation controls, and so on. Don’t concern yourself with the differences between controls right now (they’re discussed in greater detail in Session 8, “Using HTML Controls”, and Session 9, “Using Web Controls”.), just be aware that they can all respond to events, like being clicked.

Developing ASP.NET Pages

S E S S I O N

Figure 7-1 ASP.NET page events

In order to write code for these events, you need to include them in a code declaration block. In ASP.NET, a code declaration block looks like this:

<SCRIPT [LANGUAGE=”codeLangauge”] RUNAT=”SERVER” [SRC=”externalfilename”]> ‘ Event Handling Code

</SCRIPT>

The LANGUAGEattribute in the <SCRIPT>element specifies the language used in the code block. The value can be any .NET language like VB or C#. The RUNAT=”SERVER”

attribute/value pair specifies that the script block should be executed on the server-side rather than the client-side. The SRCattribute enables you to specify an external file where the code is located.

So, based on what we know at this point, an ASP.NET page should look like this:

<SCRIPT LANGUAGE=”VB” RUNAT=”server”>

Sub Page_Load(Source As Object, E As EventArgs) ‘ Page_Load Code

End Sub

Sub Control_Click(Source As Object, E As EventArgs) ‘Control_Click Code

End Sub

Sub Page_Unload(Source As Object, E As EventArgs) ‘ Page_Unload End Sub </SCRIPT> <html> <head> <title>ASP.NET Page</title> </head> <body> </body> </html>

ASP.NET files have an .aspx extension. The .aspx extension simply tells IIS that an ASP.NET page is being requested and should be handled accordingly. All code in this session should be written in files with an .aspx extension.

In ASP.NET, a page is an object, which means it has properties, events and methods. The Pageobject has one veryimportant property: isPostBack. The isPostBackproperty returns a Boolean value indicating whether the page is being loaded in response to a client post back. This is important because in many cases you will be initializing controls when a page is loaded. Since ASP.NET manages control state between requests, you probably don’t want to initialize a control if the page is responding to a post back. Right? Listing 7-1 shows an example of using the isPostBackproperty:

Listing 7-1 An isPostBack property example

<SCRIPT LANGUAGE=”VB” RUNAT=”server”>

Sub Page_Load(Source As Object, E As EventArgs) ‘ Page_Load Code

lblTest.Text = Page.isPostBack End Sub

Sub Control_Click(Source As Object, E As EventArgs) ‘Control_Click Code

End Sub

Sub Page_Unload(Source As Object, E As EventArgs) ‘ Page_Unload End Sub </SCRIPT> <html> <head> <title>ASP.NET Page</title> </head> <body>

<form ID=”frmTest” RUNAT=”SERVER”> <asp:Label ID=”lblTest” RUNAT=”SERVER”/> </BR>

<asp:Button ID=”btnSubmit” TEXT=”Submit” RUNAT=”SERVER”/> </form>

</body> </html>

As shown in Listing 7-1, we added a snippet of code to the Page_Loadevent that checks if the page is being posted back using the isPostBackproperty. Something that may look a little foreign are the server-side control declarations a little further down the page. We declared three server controls including a Form HTML control, a Label Web control and a Button Web control. You’ll notice that each of these declarations contains a “RUNAT=SERVER” attribute/value pair. This simply means that the control is rendered on the server and its events are handled on the server side rather than the client side. We’ll talk more about controls in later sessions.

The Pageclass contains many useful properties and methods. Refer to your .NET documentation for a complete treatment of the Pageclass.

OK, try running the previous example. When the page is first loaded the word “False” appears above the Submit button. If you click the Submit button, the form is posted and

Note

the page is loaded again. The word “True” now appears above the Submit button. That is because the page is being loaded in response to a post back to the server.

Now, try adding some code to the Control_Clickevent handler and wiring the Submit button to fire the Control_Clickevent handler as shown in Listing 7-2.

Listing 7-2 Using the Control_Click event handler

<SCRIPT LANGUAGE=”VB” RUNAT=”server”>

Sub Page_Load(Source As Object, E As EventArgs) ‘ Page_Load Code

lblTest.Text = Page.isPostBack End Sub

Sub Control_Click(Sender As Object, E As EventArgs) ‘Control_Click Code

Response.Write(“The Submit button was clicked!”) End Sub

Sub Page_Unload(Source As Object, E As EventArgs) ‘ Page_Unload End Sub </SCRIPT> <html> <head> <title>ASP.NET Page</title> </head> <body>

<form ID=”frmTest” RUNAT=”SERVER”> <asp:Label ID=”lblTest” RUNAT=”SERVER”/> </br>

<asp:Button ID=”btnSubmit” onClick=”Control_Click” TEXT=”Submit” RUNAT=”SERVER”/>

</form> </body> </html>

Take a look at the Button Web control declaration. You’ll notice that we added the onClick=”Control_Click”attribute/value pair. This declaration wires the btnSubmit button to fire the Control_Clickevent handling routine.

Now take a look at the Control_Clickevent handling method. When fired, by the btnSubmitWeb control, the phrase “The Submit button was clicked!” will be written to the page response. Try running the page to see what happens.