Download presentation
Presentation is loading. Please wait.
1
Introduction to ASP.Net ISYS 512
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.
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.
5
Web Server Web Server: –VS Built-in Web Server VS 08 uses the built-in web server for debugging. 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.
6
Web Project File/New Website/ –ASP.Net Empty Website –ASP.Net Website Site.Master Add a Web form: –Project/Add new item/web form A web form contains: –A design file: Ex. webform.aspx Design view and source (HTML) view –A CodeBehind file: webfor.aspx.vb To set start up page: –Point to the web page in the Solution Explorer and right click to choose Set As Start Page.
7
Create a web page to add two numbers Add controls: –Web form flow layout: Controls are positioned from left to right and from top to bottom. –To change a control’s position : Format/Set position –Absolute –Relative –Note: We can use a table to format a form in flow layout: –Table/Insert table –Add controls in the table Add code: –Double-click the form to open the code view.
8
GridView Creating bound DataGrid by dragging a table from the Server Explorer Smart tag: –Configure Data Source –Enable: Paging Soring Deleting/Editing
9
Special Folders To add a folder: right click website name and choose Add ASP.Net folder or New Folder ASP.Net folders: –App_Data:A folder to store Access database –App_Code:A folder to store classes –Bin: A folder to store assemblies –Others Regular folder: –Images:A folder to store images used in the project
10
Work with Multiple Pages Add a new form Add an existing form or page Change the starting web form Redirect or transfer to another page: –Server.Transfer(“page name”) –Response.Redirect(“page name”) –ImageButton PostBackURL property
11
HTML Introduction Heading section –,,,, etc. Body section –,, to,, –Formatting:,,, –Comment: –List –Image –Table:, : a new row in table, : a new cell in a table row. –Form:,,, – : defines a division or a section in an HTML document. tag is often used to group block-elements to format them with styles.
12
META Tag The meta tag allows you to provide additional information about the page that is not visible in the browser: – Redirection: – “3” is number of seconds.
13
TABLE Tag
14
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
15
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. Example:
16
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. – Via a form sent to the server with the GET method. Created by script
17
SCRIPT Tag Client-side script – Server-side script –
18
Input Tag HTML textbox, radiobutton, checkbox, button, listbox, etc.
19
Elements of an ASP.Net Page Directives Code blocks ASP.NET controls HTML tags and text
20
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: –
21
Inserting ASP.NET Code into Web Pages Place ASP.NET code between and with a RUNAT attribute. – Your script – ---- ASPNET/ADD2.ASPX sub clickHandler(Sender As Object, E As EventArgs) sum.text=cstr(cdbl(num1.text)+cdbl(num2.text)) end sub Inline Code Block: ASP code is placed between. – The time is now “=“ is shorthand for response.write Server-side comments: – CodeBehind file (Partial class): –
22
Inline Coding Example The time is now <% dim iHour as Double iHour=Now.Hour() if iHour < 12 then response.write(" good morning ") else response.write (" good afternoon ") end if %>
23
ASP.NET Object Model Client Server Request Object Response Object Server Object Session Object Application Object
24
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
25
<% response.write (" cid=" & request.form("cid") & " ") response.write (" cname=" & request.form("cname")& " ") Response.Write(" filepath " & Request.FilePath & " ") Response.Write(" httpMethod " & Request.HttpMethod & " ") Response.Write(" path " & Request.Path & " ") Response.Write(" Url " & Request.Url.ToString & " ") Response.Write(" urlReferer " & Request.UrlReferrer.ToString & " ") Response.Write(" HostName " & Request.UserHostName & " ") Response.Write(" HostAddress " & Request.UserHostAddress & " ")%> Note: In Request.Form(“cid”), the CID is the Name property of the tetbox, not ID property.
26
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
27
TestReqForm EnterCID: EnterName: checkbox1 checkbox 2 radio1 radio 2 A B C listbox
28
TestReqForm.Aspx <% 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")& " ") response.write (" " & request.queryString("myquery")& " ") %>
29
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”)
30
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.
31
The Application and Session Objects Application state: A central, site-wide store of variables that we can get from any page. –System.Web.HttpApplication 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. –System.Web.SessionState
32
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.
33
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()
34
ApplicationState/SessionState Properties ApplicationState: –Lock, Unlock SessionState: –SessionID –TimeOut
35
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
36
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. To add the Global.ASAX file: –Project/Add New Item/Global Application Class
37
Application/Session Demo When are Application and Session events triggered? Database: –WebLog table: StartEnd field TimeStamp field Session.abandon()
38
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\WebLogDB.accdb" Dim objConn As New OleDbConnection(strConn) Dim timeStamp As String = Now.ToString Dim strSQLInsert As String strSQLInsert = "Insert into WebLog values ('SessionEnd','" & timeStamp & "')" Dim objCommInsert As New OleDbCommand(strSQLInsert, objConn) objConn.Open() objCommInsert.ExecuteNonQuery() objConn.Close() End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\WebLogDB.accdb" Dim objConn As New OleDbConnection(strConn) Dim timeStamp As String = Now.ToString Dim strSQLInsert As String strSQLInsert = "Insert into WebLog values ('ApplicationEnd','" & timeStamp & "')" Dim objCommInsert As New OleDbCommand(strSQLInsert, objConn) objConn.Open() objCommInsert.ExecuteNonQuery() objConn.Close() End Sub
39
Global.Asax file Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\WebLogDB.accdb" Dim objConn As New OleDbConnection(strConn) Dim timeStamp As String = Now.ToString Dim strSQLInsert As String strSQLInsert = "Insert into WebLog values ('ApplicationStart','" & timeStamp & "')" Dim objCommInsert As New OleDbCommand(strSQLInsert, objConn) objConn.Open() objCommInsert.ExecuteNonQuery() objConn.Close() End Sub Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\WebLogDB.accdb" Dim objConn As New OleDbConnection(strConn) Dim timeStamp As String = Now.ToString Dim strSQLInsert As String strSQLInsert = "Insert into WebLog values ('SessionStart','" & timeStamp & "')" Dim objCommInsert As New OleDbCommand(strSQLInsert, objConn) objConn.Open() objCommInsert.ExecuteNonQuery() objConn.Close() End Sub
40
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 Sub Sub 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
41
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.
42
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
43
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
44
Example of ASP.Net Control Tag Textbox: – Properties: –Control type –ID –BackColor, ForeColor, Height, Width –Runat=“server”
45
ASP.Net Composite Controls DropdownList: –Control tag + ListItem tag Apple Orange Banana –Demo:TestListBox.aspx
46
Server Control Events Object Browser –System.Web.UI.Webcontrols
47
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.
48
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.
49
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
50
ASP.Net Controls’ AutoPostBack Property Button always triggers postback. Other controls, by default, this property is set to false.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.