Presentation is loading. Please wait.

Presentation is loading. Please wait.

Server-Side Scripting with ASP.Net ISYS 546. ASP.NET ASP.NET is a server-side technology for creating dynamic web pages. ASP.NET allows you to use a selection.

Similar presentations


Presentation on theme: "Server-Side Scripting with ASP.Net ISYS 546. ASP.NET ASP.NET is a server-side technology for creating dynamic web pages. ASP.NET allows you to use a selection."— Presentation transcript:

1 Server-Side Scripting with ASP.Net ISYS 546

2 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.

3 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.

4 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 –CodeBehind Data entered into form fields is preserved in View State.

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

6 Directives A directive controls how an ASP.Net page is compiled. –Page directives: Specify default language, enable tracing and debugging for a page. –, – –Imports name spaces –To process Access database, we need to import: –

7 Inserting ASP.NET Code into Web Pages Place ASP.NET code between and with a RUNAT attribute. – Your script – Inline Code Block: ASP code is placed between. – The time is now “=“ is shorthand for response.write Server-side comments: –<%-- Comments –--%> CodeBehind

8 Server-Side Include To include a file in an asp.net page: – Demo: timeNow.aspx

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

10 ASP.NET Request Object When a page is requested, much information is passed along with the request, such as the URL, queryString, 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.HttpRequest class.

11 Request Object Collections QueryString –http://my.com/Target.htm?CustID=C1&CustName=Chaohttp://my.com/Target.htm?CustID=C1&CustName=Chao –cid = Request.queryString(“CustID”) –cName=Request.queryString(“CustName”) Form –A form with two text boxes:CustID, CustName –cid = Request.Form(“CustID”) –cName=Request.Form(“CustName”) Cookies ClientCertificates Path, ApplicationPath, PhysicalApplicationPath, etc. Demo: testReqForm.htm, testReqForm.aspx

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

13 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=Falsesends html as it is generated.

14 Code Example: Process Form Data response.write (" cid=" & request.form("cid")& " “) response.write (" cname=" & request.form("cname")& " “) response.write (" hidden variable=" & request.form("hidden1")& " “) if request.form("C1")="ON" then response.write (" You select checkbox 1 “) end if if request.form("C2")="ON" then response.write (" You select checkbox 2 “) end if if request.form("R1")="V1" then response.write (" You select Radio 1 “) else response.write (" You select Radio 2 “) end if response.write (" listBox=" & request.form("D1")& " “) %>

15 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.

16 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.

17 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”) –myAge = Session(“Age”) To remove an item, or all items: Remove, RemoveAll() –Application.Remove(“Name”) –Session.RemoveAll()

18 The Events of the Application and Session Objects Application_OnStart –Web site start, and the first view Application_OnEnd –Web site shut down Session_OnStart Session_OnEnd

19 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.

20 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 tag. Web Form: A web page that contains: – ASP.NET server controls, and/or HTML form controls inside a tag. –ASP.NET code that produces dynamic content to be displayed within the web form.

21 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/TestRequestFormHTML.ASPX Note: Without the Action attribute, a webform is handled by itself.

22 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 DataGrid control. Rich Controls: Such as Calendar, AdRotator. Validation Controls: Such as RequiredFieldValidator. Namespace:System.Web.UI.Webcontrols

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

24 The Control An alternative way of displaying text on a web page. Properties: –ID –BackColor, ForeColor, Height, Width –Text –Visible

25 Other Intrinsic Controls –Demo: ASPNET/TestTextBox.aspx –Demo: TestDropdownlist.aspx –Demo:TestListBox.aspx

26 Server Control Events Onload: Occurs when the control has loaded into the window. onUnload OnClick: When the button control is clicked. OnInit: When the web page is first initialized.

27 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.

28 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 EventArgs) Sender: Object that raises the event E: Information about the state of the event. Page.ISPostBack property. –IF Not Page.ISPostBack Then This is the first time the page is loaded. Demo: Dialing.aspx

29 sub clickHandler(Sender As Object, E As EventArgs) mpay.text=cstr((cdbl(loan.text)*cdbl(rate.text)/12)/(1-(1+cdbl(rate.text)/12)^(- 12*cdbl(term.text)))) end sub Enter amount: Eneter interest rate: Enter term: MonthlyPayment:


Download ppt "Server-Side Scripting with ASP.Net ISYS 546. ASP.NET ASP.NET is a server-side technology for creating dynamic web pages. ASP.NET allows you to use a selection."

Similar presentations


Ads by Google