Getting Started with ASP NET Beginning ASP NET
Getting Started with ASP. NET Beginning ASP. NET 4. 5. 1 in C# and VB Chapters 1 and 2 1
Objectives You will be able to n Create a very simple web application in Visual Studio. n Dynamically alter contents of a page. n Get and process text input from the user. n Understand the relationship between what you write in Visual Studio and what the browser receives from the server. 2
ASP. NET n Active Server Pages n n ASP. NET introduced in 2002 n n n “Classic” ASP introduced in 1996 VB Script xxx. asp page files. xxx. aspx page files. xxx. aspx. cs or xxx. aspx. vb code files. ASP. NET MVC introduced 2007 3
ASP. NET n n ASP. NET permits us to dynamically create content for a web page using code that runs on the server. File of text similar to HTML. n n Can include real HTML. We can modify any properties of page elements at run time. Server translates this pseudo-HTML into real HTML which it sends to the browser. We can provide C# or VB code to respond to events such as button clicks. 4
Hello, World! n n n Let’s start with a “Hello, World” ASP. NET application. Get past the startup hurdles before trying to write real web application code. We will use Visual Studio’s built in web server to test and demonstrate our web application code. 5
Creating a New Web App n In Visual Studio, File > New Web Site n (NOT New project) Web Site vs. Project in Visual Studio. https: //msdn. microsoft. com/en-us/library/dd 547590(v=vs. 110). aspx 6
Empty Web Site Check this setting Web Site Name Check this setting 7
View Solution Explorer 8
The Solution Explorer 9
A New “Empty” Web Site n n Visual Studio is ambiguous about “project” There is no project file such as you might have seen for Visual Studio desktop programs. 10
A New “Empty” Web Site n There is a project directory 11
The Solution Files n Hello. sln holds Visual Studio settings associated with this web site (project). n n Hello. suo is a binary file n n n Hidden by default. You may not see it. Holds Visual Studio state information. These files are used only by Visual Studio. n n You can look at it with Note. Pad. Not part of the web app. Not deployed to a web server. Both of these files can be reproduced by Visual Studio at any time if they are deleted. 12 Can generally be ignored.
The Web Site Directory C: UsersturnerrDocumentsVisual Studio 2015Web. SitesHello n n n The files in this directory are the web app. Will be deployed to the web server. Will be in a virtual directory on the Internet. 13
The web. config File Double click to open in VS editor 14
The web. config File n Every ASP. NET web app has an XML configuration file called web. config. n n n Holds information used by the server and (possibly) by application code. Can be edited with any plain text editor. n n ASCII text Even on the server We will learn more about config files throughout the course. n Can ignore for now. 15
Initial web. config 16
Adding a Web Page Note: Website menu 17
Add File Default. aspx Note file name. This is the usual starting page for an ASP. NET web site. 18
Default. aspx Click here to expand this tree node. 19
Default. aspx. cs Visual Studio has also created the “code behind” file, Default. aspx. cs Double click on the file name to open it in the VS editor. 20
Default. aspx. cs The initial file doesn’t do anything. 21
Default. aspx Click on Design 22
The Design View A WYSIWYG editor for web pages. View > Toolbox 23
The Design Surface Click here to see Source view. 24
Source View Click here to return to Design view. 25
Design View Click inside Div box. Then double click on Label in Toolbox. 26
Add a Label Right click on Label. Select Properties. Set Text property to Hello, World! 27
Label Properties Click here to view in browser. 28
Page in Chrome 29
What We Wrote 30
Look at the browser’s page source In Chrome, right click in the page to open context menu. Then select "View page source". 31
What the Browser Received 32
What’s happening here? n When a browser requests an aspx page from an IIS (Microsoft) web server, IIS retrieves the file and passes it to ASPX for processing. 33
What’s happening here? n ASPX translated what we wrote into what the browser saw. n n Using the visual designer, we wrote the HTML-like code shown on a previous slide. Visual Studio’s built-in web server translated that code into the real HTML code that the browser received. n n Note that there is a single HTML form. The browser rendered the HTML as the page that we saw. 34
Translating ASPX to HTML n All <%. . . %> tags are instructions to the server. n Removed and processed by the server. <%@ Page Language="C#" Auto. Event. Wireup="true" Code. File="Default. aspx. cs" Inherits="_Default" %> 35
Translating ASPX to HTML n All <asp: xxx n n > tags are ASPX controls. Replaced by HTML as the aspx file is processed by ASPX on the server. ASPX compiles an object corresponding to each control. The object emits HTML text as the page is rendered on the server. Any HTML text is passed to the browser unchanged. 36
Translating ASPX to HTML ASPX HTML <span id="Label 1">Hello, World!</span> 37
Add Some Style n Back in the Designer n Stop the program. n n In Design view, right click on the label and select Properties window will appear. Expand Font and set Font properties n n Debug > Stop Debugging or Shift+F 5 or Click “Stop” Button Bold: True Name: Arial Size: XX-Large Set Fore. Color property to Red 38
Design View 39
Give the Page a Title Select DOCUMENT Click the Play button to display the page in the browser. 40
Our Page in Chrome Title 41
What We Wrote “Properties” became attributes in the <asp: Label> element. 42
What the Browser Received 43
The Code-Behind File n n n View Solution Explorer Expand Default. aspx Double click on Default. aspx. cs to open the source code file in an editor window. 44
Class _Default Page_Load will be executed after Default. aspx has been retrieved from the disk and before the page is rendered on the server. 45
Dynamically Alter the Page Click the Play button to display the page in the browser. 46
The New Version in Chrome 47
What We Wrote Default. aspx has not changed. 48
What the Browser Received The text inside the <span> is different now. 49
What’s happening here? n The server read file Default. aspx n The server instantiated the label control as an object. n n n The server read and compiled Default. aspx. cs, and then invoked our Page_Load method. Our code modified properties of the Label object. n n n Lable 1. Text Label 1. Fore. Color The server invoked a method of the Label object. n n With the properties that we had set. Output yourself as HTML. The browser saw the resulting HTML. End of Section 50
Getting User Input n Let’s modify the page to get the user’s name and customize the greeting for the user. n n Do this in the Page_Load event handler. Back in the Designer n Stop the program. n n n Debug > Stop Debugging or Shift+F 5 or Click “Stop” Button View the toolbox Position the cursor above <asp: Label. . . Press Enter several times to add some space above the label. Position the cursor at the top of the div. 51
Getting User Input n n n In the Toolbox, double click Label to add another Label to the page. Press Enter to move the cursor down to the next line. In the Toolbox, double click Text. Box. n n You may need to scroll down in the Toolbox. Resize the Textbox as shown on the next slide. 52
Getting User Input 53
Modify the Page in the Designer Properties that we will modify in the code behind should have meaningful names. n Give the original label a name. n n n lbl. Greeting in its (ID) Property Set text for new label to “Please enter your name then click OK. ” Set ID property of the Text. Box to tb. Name. 54
In the Designer 55
Add a Button n n Position the cursor between the textbox and Hello, World! Double click Button in Tool. Box. Set its Text property to OK. Set its ID to btn. OK. Resize the button, making it a bit larger. 56
Adding a Button 57
Adding a Button n n Double click on the button to generate an event handler for the click event. Add code to the event handler as shown in the next slide. 58
The Button-click Event Handler Click the Play button (or press F 5). 59
The Initial Page in Chrome Enter your name and then click OK. 60
After Clicking OK 61
What We Wrote
What the Browser Saw Initially 63
What the Browser Saw After the Click End of Section 64
Debugging in Visual Studio n n Click in the grey margin on the left side of a code page to set a breakpoint. When the breakpoint is hit, we can examine variables. n n And even change them! After stopping at a breakpoint, we can single step the program. 65
Breakpoints Set Click the Play button. 66
Breakpoint Hit 67
The Debug Menu 68
Examining Variables n n Drag the cursor over lbl. Greeting. Text to select it. Then right click and select Quick. Watch from the context menu that pops up. 69
Changing Variables at Breakpoints Right click on the Value and select Edit Value. 70
Changing Variable at Breakpoint Enter new value for lbl. Greeting. Text (with quotation marks. ) Press Enter then click Close. Press F 5 to continue. 71
Result End of Section 72
Files n Let’s see what files we have created. The Web. Sites Directory 73
Directory Hello n These files are plain text files. n n Can opened and edited in Note. Pad These files are the web app. n Can be copied to an IIS web server and accessed on the Internet 74
Projects Directory 75
Project Files These files are used by Visual Studio to manage the project. Remember settings, etc. Don’t need to look at them. Must not modify. Can be deleted. Visual Studio will recreate them the next time we open the web site (but any settings that we have changed will revert to their default values. ) 76
Assignment n Review the slides for this presentation. Do the examples from this lecture for yourself if you didn’t do them in class. n Read (skim) Chapters 1 and 2 of n Beginning ASP. NET 4. 5. 1 in C# and VB 77
- Slides: 77