44
Introduction to ASP.Net ISYS 512

Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

  • View
    231

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

Introduction to ASP.Net

ISYS 512

Page 2: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

Web Server

• Web Server:– Internet Information Service:

• ControlPanel/AdministrativeTools/Internet Information Services

– Built-in Web Server• VS 05 uses the built-in web server for debugging.

• IIS Default directory– C:\InetPub\wwwroot

• Default home page– Default.aspx, default.asp, default.htm

• ASP.Net project directory• Note: Using the Built-In web server, a web project

can be created in any folders.

Page 3: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

Web Project

• (Optional:) Start Internet Information server• File/New Website/ ASP.Net Application• Website folder• Web form:

– default.aspx• Design view and source (HTML) view

– Default.Aspx.VB• CodeBehind• Inline coding

• To set start up page: – Point to the web page in the Solution Explorer and right

click to choose Set As Start Page.

Page 4: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

Inline Coding Example

<body><p>The time is now <%=Now.TimeOfDay()%></p><p>The time is now <% response.write(Now.TimeOfDay())%></p><%dim iHouriHour=Now.Hour()if iHour < 12 then

response.write("<h1>good morning</h1><br>")else

response.write ("<h1>good afternoon</h1><br>")end if%>

</body>

Page 5: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

VS WebForm Design View

• Layout/Position/Auto-Position Options– HTML Designer

• CSS Positioning– Absolutely positioned

– Relative

Page 6: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

VS Web Project Demo

• Adding two numbers

• Creating bound DataGrid by dragging a table from the Server Explorer

Page 7: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

HTML Introduction

• Heading section– <head>, <title>, <meta>, <script>, etc.

• Body section– <body>, <p>, <h1> to <h6>, <a>, <br>– Formatting: <b>, <I>, <u>, <center>– Comment: <!-- comment -->– List <ul>– Image– Table: <table>, <tr>: a new row in table, <td>: a new cell

in a table row.– Form: <form>, <input>, <select>, <textarea>

Page 8: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

META Tag

• The meta tag allows you to provide additional information about the page that is not visible in the browser:– <meta name=“Author” content=“D Chao”>– <meta name=“Keywords” content=“apple,

orange,peach”>

• Redirection:– <meta http-equiv=“refresh”

content=“3;url=http://www.sfsu.edu”>• “3” is number of seconds.• Demo using FrontPage

Page 9: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

TABLE Tag

<table border="1" width="100%"> <tr> <td width="25%"></td> <td width="25%">&nbsp;</td> <td width="25%">&nbsp;</td> <td width="25%">&nbsp;</td> </tr> <tr> <td width="25%">&nbsp;</td> <td width="25%">&nbsp;</td> <td width="25%">&nbsp;</td> <td width="25%">&nbsp;</td> </tr></table>

Page 10: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

FORM Tag

• Form attribute:– Action: Specify the URL of a program on a server or an

email address to which a form’s data will be submitted.

– Method: • Get: the form’s data is appended to the URL specified by the

Action attribute as a QueryString.

• Post: A prefered method for database processing. Form’s data is sent separately from the URL.

– Name: Form’s name

• Demo: TestFormGet.Htm, TestFormPost.Htm

Page 11: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

QueryString

• A QueryString is a set of name=value pairs appended to a target URL.

• It can be used to pass information from one webpage to another.

• To create a QueryString:– Add a question mark (?) immediately after a URL.– Followed by name=value pairs separated by

ampersands (&).

• Example: • <A Href=“http://my.com/Target.htm?CustID=C1&Cname=Chao”>

Page 12: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

Creating a QueryString

• Entered with a URL:– http://dchaolaptop/testFormGet.aspx?cid=c2&cname=bbb

• As part of a URL specified in an anchor tag.– <A Href=“http://my.com/Target.htm?CustID=C1&Cname=Chao”>

• Via a form sent to the server with the GET method.

• Created by script

Page 13: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

SCRIPT Tag

• Client-side script:– <SCRIPT LANGUAGE = VBSCRIPT>

<!--

statements

-->

</SCRIPT>

• Server-side script:– <script language=“VB” runat=“server”>

• statements

– </script>

Page 14: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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.

Page 15: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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.

Page 16: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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.

– CodeBehind

Page 17: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

Elements of an ASP.Net Page

• Directives

• Code blocks

• ASP.NET controls

• HTML tags and text

Page 18: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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” %>

Page 19: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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>

• <script runat="server"> ---- ASPNET/ADD2.ASPX• sub clickHandler(Sender As Object, E As EventArgs)• sum.text=cstr(cdbl(num1.text)+cdbl(num2.text))• end sub• </script>

• Inline Code Block: ASP code is placed between <% and %>.– <p>The time is now <%=Now.TimeOfDay()%></p>– <p>The time is now <% response.write(Now.TimeOfDay())%></p>

• “=“ is shorthand for response.write• Server-side comments:

– <%-- Comments --%>• CodeBehind file (Partial class):

– <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

Page 20: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

ASP.NET Object Model

Client Server

Request Object

Response Object

Server Object

SessionObject

Application

Object

Page 21: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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.

• Demo: testRequest.Htm, TestRequest.aspx

Page 22: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

<body><%response.write ("<h1> cid=" & request.form("cid") & "</h1><br>")response.write ("<h1> cname=" & request.form("cname")& "</h1><br>") Response.Write("<h1> filepath " & Request.FilePath & "</h1><br>") Response.Write("<h1> httpMethod " & Request.HttpMethod & "</h1><br>") Response.Write("<h1> path " & Request.Path & "</h1><br>") Response.Write("<h1> Url " & Request.Url.ToString & "</h1><br>") Response.Write("<h1> urlReferer " & Request.UrlReferrer.ToString & "</h1><br>") Response.Write("<h1> HostName " & Request.UserHostName & "</h1><br>") Response.Write("<h1> HostAddress " & Request.UserHostAddress & "</h1><br>")%></body>

Note: In Request.Form(“cid”), the CID is the Name property of the tetbox, not ID property.

Page 23: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

Request Object Collections• QueryString

– http://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

Page 24: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

TestReqForm<form method="POST" action="testReqForm.aspx?myquery=testquery">

<p>EnterCID:<input type="text" name="CID" size="20"></p>

<p>EnterName:<input type="text" name="CNAME" size="20"></p>

<p><input type="checkbox" name="C1" value="ON">checkbox1&nbsp;&nbsp;&nbsp; <input type="checkbox" name="C2" value="ON">checkbox

2</p>

<p><input type="radio" value="V1" checked name="R1">radio1</p>

<p><input type="radio" name="R1" value="V2">radio 2</p>

<p><select size="1" name="D1">

<option value="A">A</option>

<option value="B">B</option>

<option value="C">C</option>

</select>listbox</p>

<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>

<input type="hidden" name="hidden1" value="Hi, it's me!">

</form>

Page 25: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

TestReqForm.Aspx<%

response.write ("<h1> cid=" & request.form("cid") & "</h1><br>")

response.write ("<h1> cname=" & request.form("cname")& "</h1><br>")

response.write ("<h1> hidden variable=" & request.form("hidden1")& "</h1><br>")

if request.form("C1")="ON" then

response.write ("<h1>You select checkbox 1</H1><br>")

end if

if request.form("C2")="ON" then

response.write ("<h1>You select checkbox 2</H1><br>")

end if

if request.form("R1")="V1" then

response.write ("<h1>You select Radio 1</H1><br>")

else

response.write ("<h1>You select Radio 2</H1><br>")

end if

response.write ("<h1> listBox=" & request.form("D1")& "</h1><br>")

response.write ("<h1>" & request.queryString("myquery")& "</h1><br>")

%>

Page 26: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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 (“…..”) *** MessageBox is not

available for web project ***.– Response.Clear(), Response.Flush(): clear/flush buffer– Response.Redirect (“URL”)

Page 27: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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.

Page 28: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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.

Page 29: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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.

Page 30: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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()

Page 31: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

ApplicationState/SessionState Properties

• ApplicationState:– Lock, Unlock

• SessionState:– SessionID– TimeOut

Page 32: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

The Events of the Application and Session Objects

• Application_Start– Web site start, and the first view

• Application_End– Web site shut down

• Session_Start

• Session_End

Page 33: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

The Global.ASAX File

• Eery ASP.NET application has this special script file.

• Must reside in the web site’s root directory.• It can contain script code that belongs to the

application, or each session.• The event handler of the application and session

objects must be placed in the Global.asax file.• Note: Website/Add New Item/Global

Page 34: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

<%@ import Namespace="System.Data" %><%@ import Namespace="System.Data.Oledb" %><script runat="server"> Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String Dim objDataReader As OleDbDataReader Dim objComm As New OleDbCommand() Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) objConn.Open() strSQL = "select VisitorCounter from VCounterTable where CounterDate = (" strSQL = strSQL + "Select Max(CounterDate) from VCounterTable);" Dim objComm As New OleDbCommand(strSQL, objConn) objDataReader = objComm.ExecuteReader() objDataReader.Read() Application("visitor") = objDataReader("VisitorCounter") objConn.Close() End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) Dim objComm As New OleDbCommand() objConn.Open() objComm.Connection = objConn objComm.CommandType = CommandType.Text strSQL = "Insert Into VCounterTable Values (#" + CStr(DateTime.Now()) + "#, " + CStr(Application("visitor")) + ");" objComm.CommandText = strSQL objComm.ExecuteNonQuery() objConn.Close() End SubSub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Application.Lock() Application("visitor") = Application("visitor") + 1 Session("visitor") = Application("visitor") Application.UnLock() End Sub Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when a session ends. End Sub</script>

Page 35: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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.

Page 36: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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 the server occurs.– Page_Init

– Page_Load: Occurs when a page is visible.

– Control Events

– Page_Unload

• Note: A webform is handled by itself.• Demo: web form with regular HTML controls –

ASPNET/TestRequestFormHTML.ASPX

Page 37: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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

Page 38: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

Example of ASP.Net Control Tag

• Textbox:– <asp:TextBox ID="TextBox1" runat="server"

Style="z-index: 100; left: 80px; position: absolute; top: 64px"></asp:TextBox>

• Properties:– Control type– ID– BackColor, ForeColor, Height, Width– Runat=“server”

Page 39: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

ASP.Net Composite Controls• DropdownList:

– Control tag + ListItem tag•<asp:dropdownlist id="list1" runat="server">

•<asp:listitem>Apple</asp:listitem>•<asp:listitem>Orange</asp:listitem>•<asp:listitem>Banana</asp:listitem>•</asp:dropdownlist>

• <asp:listbox/>– Demo:TestListBox.aspx

• <asp:radiobuttonlist/>

• <asp:checkboxlist/>

Page 40: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

Server Control Events

• Object Browser– System.Web.UI.Webcontrols

Page 41: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

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.

Page 42: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

The effects of Postback

• Remember the state of the form by adding a hidden _VIEWSTATE variable.

• Enable to write event handler.• Page.ISPostBack property.

– IF Not Page.ISPostBack Then• This is the first time the page is loaded.

• Demo: Display welcome message only once.

Page 43: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Response.Write("Welcome to this demo page") End If End Sub

Page 44: Introduction to ASP.Net ISYS 512. Web Server Web Server: –Internet Information Service: ControlPanel/AdministrativeTools/Internet Information Services

ASP.Net Controls’ AutoPostBack Property

• Button always triggers postback.

• Other controls, by default, this property is set to false.