Module 5 Net 2005
Module 5 Net 2005
Module 5
Module 5: Adding Code to a MS ASP.NET Web Form Youll learn about the various methods that can be used for adding code to your MS ASP.NET Web application Plus! youll learn about event procedures for Web server controls, how to use them, and the order which they work Then, learn how to use code-behind pages for adding code to Web pages
Overview
Using Code-Behind Pages
How you put in VB.NET in Web Form??? & How its work???
Inline Code
Separate SCRIPT section of the same file as HTML content
Code-behind
Code is in a separate file from the HTML content The code called code-behind-page
<HTML> <asp:Button id="btn" runat="server"/> </HTML> <SCRIPT Language="vb" runat="server"> Sub btn_Click(s As Object, e As EventArgs) Handles btn.Click ... End Sub </SCRIPT> <HTML> <asp:Button id="btn" runat="server"/> </HTML> <SCRIPT Language="c#" runat="server"> protected void btn_Click(object sender, System.EventArgs e) { . . . } </SCRIPT>
Design
UI (HTML)
code <tags>
Form1.aspx
<tags>
Form1.aspx
code
Form1.aspx.vb or Form1.aspx.cs
Each Web page contain all of the programming logic for a single Web page Each Web page in a Web application has its own code-behind page By default, a code-behind page has the same name as the Web page with which it is associated; however, the code-behind page also has an .aspx.vb or .aspx.cs
logic
Use @ Page directive to link the two files Pre-compile or JIT-compile
Default.aspx.vb
Default.aspx
<%@ Page Language="VB"
Partial Class _Default protected Sub cmd1_Click() End Sub End Class
An example @Page
VB.NET
<%@ Page Language=vb CodeFile=Page1.aspx.vb Inherits=_Default %>
C#
<%@ Page Language=c# CodeFile=Page1.aspx.cs
Inherits=_Default %>
Event Procedures
Dynamic, interactive Web Forms typically react to user input
1- CLIENT 2- SERVER-SIDE
Does not have access to server resources action done on client-side (no access to server!)
Time-saving & performance Uses <SCRIPT language="language">
Internet
.HTM Pages
CLIENT-SIDE (validation)
Client-side event procedures Are event that are handled on the computer that request the Web Form (the client) When an event is generated no information is sent to server For example, you cannot use client-side script to access a SQL Server database The client browser interprets the code and perform actions Uses for client-side event procedures Are useful for event that you want to happen immediately because they do not require a round trip to the Web server (sending information to the Web server and waiting for a response) Do not send information to Web server and wait for response
Internet
.ASPX Pages
side event procedures require information to be sent to the Web server for processing
Although these is a time cost to using serverside event procedures, they are much
SERVER-SIDE
Server-side event procedures
Consists of compiled code that exist on the web server Can be used to handle events that are generated from both Web and HTML server controls
4. Because no information has been sent to the server, client-side processing reduces network traffic and response times.
8. Because the client-side script cannot access server resources, server-side processing offers a greater range of security & flexibility in data processing.
protected WithEvents cmd1 As System.Web.UI.WebControls.Button protected Sub cmd1_Click(ByVal s As System.Object, _ ByVal e As System.EventArgs) Handles cmd1.Click
protected System.Web.UI.WebControls.Button cmd1; protected void InitializeComponent() { this.cmd1.Click += new System.EventHandler(this.cmd1_Click); this.Load += new System.EventHandler(this.Page_Load); } protected void cmd1_Click(object s, System.EventArgs e)
Using the Handles keyword adds many event procedures to one event
Object
Is the object firing of the event
EventArgs
Is specific information for the event
When you double-click a control in VS .NET, VS .NET declares a variable (with the same name as the id attribute of the control) and creates an event procedure template When you use VB.NET, VS.NET also adds the Handles keyword, which attaches the event procedure to the control The Handles keyword allows you to create multiple event procedures for a single event
an
Event Procedure
In many Web applications, you need to read from and write to controls on a form You can do this within server-side event procedures Within a server-side event procedure, you can read information from a server control For example, if you have the following form with a Textbox and a Button control:
<FORM id=Form1 runat=server> <asp:TextBox id=txtName runat=server /> <asp:Button id=cmd1 runat=server /> </FORM>
When the user clicks the button, you can read the text that the user typed into the text box The following code assigns the string variable strGreeting to a concatenation of the text Hello and the text in the txtName text box:
VB.NET Dim strGreeting As String = Hello & txtName.Text C# string strGreeting = Hello + txtName.Text
For example, if a user typed Ahmad in the txtName text box, the strGreeting variable would contain the text string Hello Ahmad.
The page event life cycle consists of the following page events, which occur in the following order: 1. Page_Init. This page event initializes the page by creating and initializing the Web server controls on the page. (start loading the page start / boot) 2. Page_Load. This page event runs every time the page is requested. 3. Control events. This page event includes change events (for example,Textbox1_Changed) and action events (for example, Button1_Click). 4. Page_Unload. This page event occurs when the page is closed or when the control is passed to another page. (close the page)
The end of the page event life cycle includes the disposal of the page from memory
Postbacks
In ASP.NET, forms are designed to post information back to the sending ASP.NET page for processing This process is called postback Postbacks may occur with certain user actions By default, only Button click events cause form to be posted back to the server However, of you set the AutoPostBack property of a control true, a postback is forced for events of that control
AutoPostBack="True">
<asp:ListItem>First <asp:ListItem>Second </asp:DropDownList> Choice</asp:ListItem> Choice</asp:ListItem>
Protected Sub ListBox1_SelectedIndexChanged _ (ByVal s As System.Object, ByVal e As System.EventArgs) _ Handles ListBox1.SelectedIndexChanged TextBox1.Text=ListBox1.SelectedItem.Value End Sub
Postback Forms
User request a page from a server server returns the page to the user In the page
ListBox Label Submit Button
When user change the solution in the list box and then clicks the submit button, the information is sent back to server If you want the new value of the list
box to be sent to the server immediately dont have to wait for user to click Submit button
box.
2. The server then returns the page to the user. In this example, the page has a ListBox, a blank Label, and a Submit button on it. 3. When the user changes the selection in the list box, and then clicks the Submit button, the information is sent back to the server.
soon as the user changes the selection in the list box, the information is sent to the server.
9. The server updates the label to reflect the change, and then sends the updated information back to the client.
Demonstration:
Handling Events
In Order
Review
Using Code-Behind Pages