Download presentation
Presentation is loading. Please wait.
Published byBeverley McDaniel Modified over 9 years ago
1
Introduction to ASP.Net ISYS 512
2
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.
3
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.
4
Web Server Web Server: –VS Built-in Web Server localhost ASP.Net project directory –Using the Built-In web server, a web project can be created in any folder of your choice.
5
ASP.NET Object Model Client Server Request Object Response Object Server Object Session Object Application Object
6
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.
7
Request Object Properties 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.
8
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”)
9
Web Project New Project: –C#/Web/Asp.Net Empty Web Application Add a Web form: –Project/Add new item/web form A web form contains: –A design file: Ex. webform.aspx Design view Source (HTML) view To set start up page: –Point to the web page in the Solution Explorer and right click to choose Set As Start Page.
10
Two Types of Form HTML Form with HTML controls: EnterCID: ASP.Net Web Form with server-side controls: <asp:TextBox ID="TextBox2" runat="server" style="position: absolute; top: 39px; left: 164px; z- index: 1">
11
Web Form Demo: Create an ASP.Net Web Form 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. –Events: Page_Load event and control’s events
12
Elements of an ASP.Net Page Design page: with file extension aspx –Contains: Directives ASP.Net controls HTML tags Code-behind file: with file extension aspx.cs or aspx.vb
13
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: –
14
Example of ASP.Net Control Tag Textbox: – Properties: –Control type –ID –BackColor, ForeColor, Height, Width –Runat=“server”
15
ASP.Net Controls: Textbox, listbox, radiobutton list, button, dropdownlist
16
Code Example double pv, rate, year, fv; pv = double.Parse(TextBox1.Text); rate =double.Parse( ListBox1.SelectedValue); year = double.Parse(RadioButtonList1.SelectedValue); fv = pv * Math.Pow(1 + rate, year); TextBox2.Text = fv.ToString("c");
17
ASP.Net Composite Controls Example: DropdownList: –Control tag + ListItem tag 5% 6% 7%
18
WebForm Wizard Drag/Drop a table from Server Explorer to create bound GridView control automatically.
19
Bound GridView Creating bound DataGrid by dragging a table from the Server Explorer Smart tag: –Configure Data Source –Enable: Paging: records may be displayed in many pages Sorting Deleting Editing: –Update/Cancel
20
Work with Multiple Pages Add a new web form Choose the starting web form Redirect or transfer to another page: –Use Button click event: Server.Transfer("WebForm2.aspx"); Or, Response.Redirect("WebForm2.aspx"); –Use ImageButton: ImageURL PostBackURL: WebForm2.aspx –Use linkButton: PostbackURL Text
21
Working with a HTML Page Adding a HTML page: –Project/Add New Item/HTML page
22
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.
23
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.
24
TABLE Tag
25
HTML 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
26
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:
27
Input Tag HTML textbox, radiobutton, checkbox, button, listbox, etc.
28
ASP.Net Inline Coding Example The time is now The time is now %> <% double iHour; iHour= DateTime.Now.Hour; if (iHour < 12) Response.Write(" good morning "); else Response.Write (" good afternoon "); %>
29
A HTML Form submitted using the Get method and is handled by an ASP.Net page Demo: –testFormGet.htm –testFormGet.aspx
30
testFormGet.htm EnterCID: EnterName:
31
testFormGet.aspx <% Response.Write(" " + Request.QueryString["cid"] + " "); Response.Write (" " + Request.QueryString["cname"] + " "); %>
32
A HTML Form submitted using the Post method and is handled by an ASP.Net page Demo: –testFormPost.htm –testFormPost.aspx
33
testFormPost.htm EnterCID: EnterName:
34
testFormPost.aspx <% Response.Write (" cid=" + Request.Form["cid"] + " "); Response.Write (" cname=" + Request.Form["cname"] + " ");%>
35
Other ASP.NET Request Object Properties Demo Demo: –testRequest.Htm –TestRequest.aspx
36
testRequest.htm EnterCID: EnterName:
37
<% 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 + " "); %> testRequest.aspx
38
Demo other HTML controls: TestReqForm EnterCID: EnterName: checkbox1 checkbox 2 radio1 radio 2 A B C listbox
39
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") Response.Write (" You select checkbox 1 "); if (Request.Form["C2"]=="ON") Response.Write (" You select checkbox 2 "); if (Request.Form["R1"]=="V1") Response.Write (" You select Radio 1 "); else Response.Write (" You select Radio 2 "); Response.Write (" listBox=" + Request.Form["D1"] + " "); Response.Write(" " + Request.QueryString["myquery"] + " "); %>
40
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.
41
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
42
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.
43
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[“CustName”]=“Smith”; –Session [“Age”]=25; To read values from the Application and Session: –Cname=Application[“Name”].ToString(); –myAge = Session[“Age”].ToString(); To remove an item, or all items: Remove, RemoveAll() –Application.Remove[“Name”]; –Session.RemoveAll();
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.