Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.

Similar presentations


Presentation on theme: "© Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML."— Presentation transcript:

1 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. mchen@gmu.edu.NET Framework Base Class Library ADO.NET: Data & XML Windows Form: Windows User Interface Common Language Runtime ASP.NET: Web Forms & Web Services

2 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 2.NET Framework Operating System Common Language Runtime Base Class Library ADO.NET and XML ASP.NET Web Forms Web Services Mobile Forms ASP.NET Web Forms Web Services Mobile Forms WindowsFormsWindowsForms Common Language Specification VBVBC++C++C#C#JScriptJScriptJ#J# Visual Studio.NET

3 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 3 Create a New Web Site

4 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 4 Creating a New Page

5 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 5 Select a Web Form

6 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 6 Form Handling: No Web Server Control Untitled Page Enter your name: " & _ Request.Params.Get("username") & " ")%> End of the active content Mixing HTML and Server side scripting code Submit to the form itself (Postback) since there is no action attribute

7 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 7

8 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 8 Toolbox – Web Server Controls

9 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 9 Properties Window

10 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 10

11 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 11 Create an Event Procedure Double click on a Web Server Control Determine the event to be associated with the control Write appropriate code for responding to the event

12 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 12 Set a Breakpoint for Debugging Click on the sideline of the corresponding code line to set the breakpoint Click again to remove the breakpoint

13 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 13 Set as Start Page Your project/web site may contain many Web forms. When you try to run and test a specific Web page, you should set that page to be the Start page. Right mouse button click on the page, choose "Set As Start Page" from the pop- up menu

14 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 14 Running and Debugging your Web Form

15 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 15 Web.config file You need to Add a new Web.config file for the whole Web site / Web application

16 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 16 Web.config

17 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 17 Web Application Administration Hand editing this file or use the web admin tool to configure settings for your application. Use the Website->Asp.Net Configuration option in VWD A full list of settings and comments can be found in machine.config.comments usually located in \Windows\Microsoft.Net\Framework\v2.x\Config

18 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 18 Running the Web page

19 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 19 Debugging Functions

20 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 20 Error List Include all errors from Web forms in the Web site Enter Your Name: Just say NO!

21 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 21

22 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 22 Add Watch Highlight a variable, an object, or a property Right mouse button click to select the "Add Watch" from the menu

23 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 23 Page Load Event Page Load event will be executed every time you access the page. Web server controls' events always post back to the page itself. Use IsPostBack function to determine whether it is the first request of the page or a post back to the page. action ASP.NET page's tag does not have the action attribute, therefore all Web server controls event will send the form variables (i.e., Web server controls' data) to the same page to be processed by corresponding server-side event-handling methods. Postback

24 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 24 PostBack Protected Sub Page_Load ( ByVal sender As Object, ByVal e As System.EventArgs) If IsPostBack Then LabelWelcome.Text = "Thank you, " & TextBox1.Text & _ " for using Hello World!" Else LabelWelcome.Text = "Welcome to my Hello World!" End If End Sub Protected Sub Button1_Click(…..) Label1.Text = "Hello " & TextBox1.Text End Sub

25 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 25 PostBack First Request Page/Control Event ExecutionPage_Load Page_PreRenderPage_Unload Textbox1_Changed Button1_Click 1. Change Events 2. Action Events Page DLL is loaded, control hierarchy initialized Page is disposed Control hierarchy (Dynamically generated HTML page) is rendered They may be triggered on PostBack

26 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 26 ASP.NET Pages: Part Declarative, Part Code Combines declarative tags (HTML, ASPX directives, server controls tags, and static text) with code in a single file or in separate files. Unlike ASP, good separation provided between code and tags Form1.aspxForm1.aspxForm1.aspx.vb single fileseparate files (“code-behind”) code <tags> code <tags>

27 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 27 Control Event Processing Events are: –Triggered on the client by the user –Handled in server code Requires a postback to the same pageRequires a postback to the same page ViewState of controls saves page and control properties between round trips, therefore helps restore control to its previous state. –Implemented as a hidden form field –Disable via setting the EnableViewState attribute  EnableViewState=false –Data Binding resets control state

28 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 28 How Do Server Controls Work? Declared with runat="server" Attribute When the ASP.NET Page is executed: –Creates action and method attributes of form –Adds unique id and name attributes to controls (id is used by ASP.NET and name is used by HTML form controls) –Adds value attribute to controls (to set the default value for initial display) –Adds a hidden control to the form to save view state information

29 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 29 Generated Generated HTML Source Code on First Request of Hello2.aspx Hello2 <input type="hidden" name="__VIEWSTATE" value="dDwtMTM3NjQ2NjY2NTs7PmhdqcuyzroBSmx2I5btO60KJNMH" /> Enter your name: name="TextBox1"id="TextBox1" First Time

30 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 30 Page Generated After Submission (Postback) Hello2 Enter your name: value="Minder Chen" Minder Chen Post back

31 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 31 ViewState and PostBack Hello2.aspx.vb If Not IsPostBack … First Request Response __VIEWSTATE = " dDwt…" Minder Chen Post back to the same page __VIEWSTATE = " dDwt…" Textbox1.Text = "Minder Chen" States of a page is maintained via the ViewState between the Postback Hello2.aspx __VIEWSTATE = " dDwt…" Response Default value

32 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 32 RadioButtonList.aspx runtime

33 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 33 Web Form Designer RadioButtonList Checkbox with AutoPostBack Panel Visible = False

34 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 34 Set Up Properties of Web Server Controls

35 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 35 Define Items in RadioButtonList control

36 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 36 RadioButtonList.aspx Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) If CheckBox1.Checked Then Label1.Text = "You know ASP.NEt 2.0 " Label1.Text &= "Your level of expertise is: " & RadioButtonList1.SelectedItem.Text _ & " " Label1.Text &= "Your level of expertise code is: " & RadioButtonList1.SelectedValue Else Label1.Text = "You don't know ASP.NET 2.0 " End If End Sub Protected Sub CheckBox1_CheckedChanged (ByVal sender As Object, ByVal e As System.EventArgs) If CheckBox1.Checked Then Panel1.Visible = True Else Panel1.Visible = False End If End Sub Protected Sub Page_Load (ByVal sender As Object, ByVal e As System.EventArgs) Label1.Text = " " ' Reset error message End Sub Panel1.Visible = Not Panel1.Visible

37 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 37 Code -- Continued RadioButtonList Auto Postback + Checkbox + DadioButtonList <asp:Panel ID="Panel1" runat="server" BorderStyle="Outset" Height="136px" Visible="False" Width="256px"> Choose your level of expertise in ASP.NET 2.0 Basic Intermediate Advanced

38 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 38 Add.aspx Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) ' Label1.Text = TextBox1.Text + TextBox2.Text Dim x1, x2, result As Single x1 = CType(TextBox1.Text, Single) x2 = CType(TextBox2.Text, Single) result = x1 + x2 ' Format String Expression "c" for currency format ' Label1.Text = Format(result, "c") Label1.Text = result.ToString("C") End Sub Untitled Page Add two numbers Number 1: Plus (+) Number 2: Answer:

39 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 39 Add.aspx with Exception Handling Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) ' Label1.Text = TextBox1.Text + TextBox2.Text Dim x1, x2, result As Single Label1.Text = "" Try x1 = CType(TextBox1.Text, Single) Catch ex As Exception Label1.Text = "Number 1 is not a number. " & ex.Message & " " End Try Try x2 = CType(TextBox2.Text, Single) Catch ex As Exception Label1.Text &= “Number 2 is not a number. " & ex.Message End Try If Label1.Text <> "" Then Exit Sub End If result = x1 + x2 ' Format String Expression "c" for currency format ' Label1.Text = Format(result, "c") Label1.Text = result.ToString("C") End Sub

40 © Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 40 Runtime Compilation ASPX File Request ASPXEngine Parse Gen’d Page ClassGenerate Response Request Instantiate Response Code- behind class PageClass Instantiate, Process and Render Compile


Download ppt "© Minder Chen, 2001-2005 ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML."

Similar presentations


Ads by Google