ServerSide Scripting with ASP Net ISYS 546 ASP

  • Slides: 29
Download presentation
Server-Side Scripting with ASP. Net ISYS 546

Server-Side Scripting with ASP. Net ISYS 546

ASP. NET • ASP. NET is a server-side technology for creating dynamic web pages.

ASP. NET • ASP. NET is a server-side technology for creating dynamic web pages. • ASP. NET allows you to use a selection of full programming languages. The default language is VB. NET. • ASP. NET files have a. aspx extension.

ASP. NET in the. NET Framework • 1. The client requests a web page.

ASP. NET in the. NET Framework • 1. The client requests a web page. • 2. The web server locates the page. • 3. If the page is an ASP. NET page, it is sent to the Common Language Runtime for compilation and execution. • 4. The HTML produced by the CLR is returned to the browser.

Benefits of Server-Side Technology • Browser compatibility: Every browser reads HTML. • Protection of

Benefits of Server-Side Technology • Browser compatibility: Every browser reads HTML. • Protection of source code. • Controls are server-side objects with properties, methods and events. • Separating code from content. – Embedded code – Code. Behind • Data entered into form fields is preserved in View State.

Elements of an ASP. Net Page • • • Directives Code blocks ASP. NET

Elements of an ASP. Net Page • • • Directives Code blocks ASP. NET controls HTML tags and text. Server-side include directives

Directives • A directive controls how an ASP. Net page is compiled. – Page

Directives • A directive controls how an ASP. Net page is compiled. – Page directives: Specify default language, enable tracing and debugging for a page. – <%@ Page Language=“VB” %>, <%@ Page Language=“C#” %> – <%@ Page Trace=“True” %> – – Imports name spaces To process Access database, we need to import: <%@ Import Namespace=“System. Data” %> <%@ Import Namespace=“System. Data. Oledb” %>

Inserting ASP. NET Code into Web Pages • Place ASP. NET code between <script>

Inserting ASP. NET Code into Web Pages • Place ASP. NET code between <script> and </script> with a RUNAT attribute. – <script language=“VB” runat=“server”> • Your script – </script> • Inline Code Block: ASP code is placed between <% and %>. – <p>The time is now <%=Now. Time. Of. Day()%></p> – <p>The time is now <% response. write(Now. Time. Of. Day())%></p> • “=“ is shorthand for response. write • Server-side comments: – <%- • Comments – --%> • Code. Behind

Server-Side Include • To include a file in an asp. net page: – <!--

Server-Side Include • To include a file in an asp. net page: – <!-- #include file="testreqformnet. aspx" --> • Demo: time. Now. aspx

ASP. NET Object Model Client Request Object Server Object Response Object Server Application Object

ASP. NET Object Model Client Request Object Server Object Response Object Server Application Object Session Object

ASP. NET Request Object • When a page is requested, much information is passed

ASP. NET Request Object • When a page is requested, much information is passed along with the request, such as the URL, query. String, and data from a form. The request object allows you to get the information passed along with the request. • It is created from the System. Web. Http. Request class.

Request Object Collections • Query. String – http: //my. com/Target. htm? Cust. ID=C 1&Cust.

Request Object Collections • Query. String – http: //my. com/Target. htm? Cust. ID=C 1&Cust. Name=Chao – cid = Request. query. String(“Cust. ID”) – c. Name=Request. query. String(“Cust. Name”) • Form – A form with two text boxes: Cust. ID, Cust. Name – cid = Request. Form(“Cust. ID”) – c. Name=Request. Form(“Cust. Name”) • Cookies • Client. Certificates • Path, Application. Path, Physical. Application. Path, etc. • Demo: test. Req. Form. htm, test. Req. Form. aspx

ASP. NET Response Object • This object allows you to send information back to

ASP. NET Response Object • This object allows you to send information back to client. • It is created from the System. Web. Http. Response class. • Properties: – Buffer – Cookies (a collection) • Methods: – Response. Write (“…. . ”), Response. Write. File( ) – write the contents of a file to the HTML output stream. – Response. Clear(), Response. Flush(): clear/flush buffer – Response. Redirect (“URL”) – Demo: Test. Req. Form. asp

Buffer • When ASP. Net is running the code, it gradually builds up the

Buffer • When ASP. Net is running the code, it gradually builds up the HTML that will be sent back to the browser. As the HTML is generated, it is placed in a buffer. Normally, the HTML is held in the buffer so that it isn’t sent to the browser until the page finishes executing. • Response. Buffer: The default value for this property is true which means the page is buffered and sent in one block. – Response. Buffer=False sends html as it is generated.

Code Example: Process Form Data response. write ("<h 1> cid=" & request. form("cid")& "</h

Code Example: Process Form Data response. write ("<h 1> cid=" & request. form("cid")& "</h 1> “) response. write ("<h 1> cname=" & request. form("cname")& "</h 1> “) response. write ("<h 1> hidden variable=" & request. form("hidden 1")& "</h 1> “) if request. form("C 1")="ON" then response. write ("<h 1>You select checkbox 1</H 1> “) end if if request. form("C 2")="ON" then response. write ("<h 1>You select checkbox 2</H 1> “) end if if request. form("R 1")="V 1" then response. write ("<h 1>You select Radio 1</H 1> “) else response. write ("<h 1>You select Radio 2</H 1> “) end if response. write ("<h 1> list. Box=" & request. form("D 1")& "</h 1> “)

The Application and Session Objects • Application state: A central, site-wide store of variables

The Application and Session Objects • Application state: A central, site-wide store of variables that we can get from any page. • A session is a single visit to a web site, and normally includes visits to a number of pages. Each time a visitor comes to your web site, a session object is created for the visitor. Session state is a store of variables that relates to a session.

Examples of Using the Application and Session Objects • Examples of session variables are:

Examples of Using the Application and Session Objects • Examples of session variables are: user’s id, user’s name, Shopping cart, etc. • Examples of application variables are: visitor counter, number of login attempts, etc.

Working with the Application and Session • To place a value into the Application

Working with the Application and Session • To place a value into the Application and Session simply assign it a key and then assign the value: – Application (“Name”)=“Smith” – Session (“Age”)=25 • To read values from the Application and Session: – Cname=Application(“Name”) – my. Age = Session(“Age”) • To remove an item, or all items: Remove, Remove. All() – Application. Remove(“Name”) – Session. Remove. All()

The Events of the Application and Session Objects • Application_On. Start – Web site

The Events of the Application and Session Objects • Application_On. Start – Web site start, and the first view • Application_On. End – Web site shut down • Session_On. Start • Session_On. End

The Global. ASAX File • Every ASP. NET application has this special script file.

The Global. ASAX File • Every ASP. NET application has this special script file. • Must reside in the web site’s root directory. • It can contain script code that pertains to the application, or each session. • The event handler of the application and session objects must be placed in the Global. asax file. – As the user browses your site, utilize the information regarding the user’s preferences to display information. • Note: Use a text editor to edit Global. ASAX file.

Web Form vs HTML Form • HTML Form: A web page that contains one

Web Form vs HTML Form • HTML Form: A web page that contains one or more HTML form controls such as textbox, checkbox, dropdown list, and button inside an HTML <form> tag. • Web Form: A web page that contains: – ASP. NET server controls, and/or HTML form controls inside a <form runat=“server”> tag. – ASP. NET code that produces dynamic content to be displayed within the web form.

Web Form Events • Every time a page is called the page object goes

Web Form Events • Every time a page is called the page object goes through a series of stage: initializing, loading, processing and disposing of information. It happens every time a round trip to theserver occurs. – – Page_Init Page_Load: Occurs when a page is visible. Control Events Page_Unload • Demo: web form with regular HTML controls – ASPNET/Test. Request. Form. HTML. ASPX • Note: Without the Action attribute, a webform is handled by itself.

ASP. NET Server Controls • Intrinsic Controls: These controls correspond to their HTML counterparts.

ASP. NET Server Controls • Intrinsic Controls: These controls correspond to their HTML counterparts. – Ex. Textbox, listbox, button, etc. • Data-Centric Controls: Controls used for binding and displaying data from a data source, such as the Data. Grid control. • Rich Controls: Such as Calendar, Ad. Rotator. • Validation Controls: Such as Required. Field. Validator. • Namespace: System. Web. UI. Webcontrols

Benefits of Using ASP. NET Intrinsic Server Controls • They are programmable objects. –

Benefits of Using ASP. NET Intrinsic Server Controls • They are programmable objects. – Respond to events – Have properties

The <asp: label> Control • An alternative way of displaying text on a web

The <asp: label> Control • An alternative way of displaying text on a web page. • Properties: – ID – Back. Color, Fore. Color, Height, Width – Text – Visible

Other Intrinsic Controls • <asp: textbox/> – Demo: ASPNET/Test. Text. Box. aspx • <asp:

Other Intrinsic Controls • <asp: textbox/> – Demo: ASPNET/Test. Text. Box. aspx • <asp: dropdownlist/> – Demo: Test. Dropdownlist. aspx • <asp: listbox/> – Demo: Test. List. Box. aspx • <asp: radiobuttonlist/> • <asp: checkboxlist/> • <asp: button/>

Server Control Events • Onload: Occurs when the control has loaded into the window.

Server Control Events • Onload: Occurs when the control has loaded into the window. • on. Unload • On. Click: When the button control is clicked. • On. Init: When the web page is first initialized.

Postback • Postback is the process by which the browser posts information back to

Postback • Postback is the process by which the browser posts information back to the server telling the server to handle the event, the server does so and sends the resulting HTML back to the browser.

The effects of Postback • Remember the state of the form by adding a

The effects of Postback • Remember the state of the form by adding a hidden _VIEWSTATE variable. • Enable to write event handler. – Sub dialing(Sender as object, E as Event. Args) • Sender: Object that raises the event • E: Information about the state of the event. • Page. ISPost. Back property. – IF Not Page. ISPost. Back Then • This is the first time the page is loaded. • Demo: Dialing. aspx

<script runat="server"> sub click. Handler(Sender As Object, E As Event. Args) mpay. text=cstr((cdbl(loan. text)*cdbl(rate.

<script runat="server"> sub click. Handler(Sender As Object, E As Event. Args) mpay. text=cstr((cdbl(loan. text)*cdbl(rate. text)/12)/(1 -(1+cdbl(rate. text)/12)^(12*cdbl(term. text)))) end sub </script> <html> <body> <asp: label id="message" runat="server" /> <form runat="server"> Enter amount: <asp: textbox id="Loan" runat="server" /> Eneter interest rate: <asp: textbox id="Rate" runat="server" /> Enter term: <asp: textbox id="Term" runat="server" /> Monthly. Payment: <asp: textbox id="MPay" runat="server" /> <asp: Button id="Button 1" Text="Submit" on. Click="Click. Handler" runat="server"/> </form> </body> </html>