11 ASP.NET Server Controls Beginning ASP.NET 4.5.1 in C# and VB Chapter 4.

Slides:



Advertisements
Similar presentations
Microsoft Expression Web-Illustrated Unit J: Creating Forms.
Advertisements

CS0004: Introduction to Programming Visual Studio 2010 and Controls.
11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
11 ASP.NET Controls II Beginning ASP.NET 4.0 in C# 2010 Chapter 6.
Web Development in Microsoft Visual Studio Slide 2 Lecture Overview Introduce Visual Studio 2013 Create a first ASP.NET application.
Tutorial 6 Working with Web Forms
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
11 ASP.NET Controls Beginning ASP.NET 4.0 in C# 2010 Chapter 6.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
CST JavaScript Validating Form Data with JavaScript.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
4-Sep-15 HTML Forms Mrs. Goins Web Design Class. Parts of a Web Form A Form is an area that can contain Form Control/Elements. Each piece of information.
Forms and Form Controls Chapter What is a Form?
CSE 382/ETE 334 Internet and Web Technology Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
JavaScript & jQuery the missing manual Chapter 11
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
Beginning Web Site Development Module 1 – Dynamic Web Site Development Fundamentals of building dynamic Web sites with ASP.NET 2.0 and C# Version.
1 HTML Forms. 22 HTML forms provide a way for a user to send information back to the web server. Originally the user input was processed on the server.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
CSCI 6962: Server-side Design and Programming Introduction to Active Server Pages.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
1 Chapter 12 – Web Applications 12.1 Programming for the Web, Part I 12.2 Programming for the Web, Part II 12.3 Using Databases in Web Programs.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
Dynamic Dropdown Lists 1. Objectives You will be able to Use Dropdown Lists to solicit multiple choice user input in an ASPX web page. Populate a Dropdown.
PHP Form Introduction Getting User Information Text Input.
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Chapter 4: Working with ASP.NET Server Controls OUTLINE  What ASP.NET Server Controls are  How the ASP.NET run time processes the server controls on.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Bookstore Application: Middle Tier Introducing Code-Behind Files, Session State.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
HTML FORMS The TEXT Object Presented By: Ankit Gupta.
1 HTML Forms. Objectives You will be able to: Compose HTML forms, to accept input from the user. Identify the different kinds of input tags that can appear.
1111 Creating ASPX Controls Programatically Objectives You will be able to Dynamically add controls to a page. Dynamically alter properties of controls.
11 Getting Started with ASP.NET Beginning ASP.NET in C# and VB Chapters 1 and 2.
Copyright (c) 2004 Prentice-Hall. All rights reserved. 1 Committed to Shaping the Next Generation of IT Experts. Project 6: Creating XHTML Forms Kelly.
1111 Creating HTML Programatically Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML.
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
1111 Master Pages Beginning ASP.NET in C# and VB Chapter 6.
11 User Controls Beginning ASP.NET in C# and VB Chapter 8.
Creating a Web Site Creating a new Web site Defining and using folders Creating and editing Web pages Viewing pages in a Web browser.
Beginning ASP.NET in C# and VB Chapter 9
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Chapter 5 Validating Form Data with JavaScript
ASP.NET Forms.
Web Development in Microsoft Visual Studio 2013
Static and Dynamic Web Pages
ASP.NET.
Module 05: Building ASP .NET Applications
5/6/2019 Session 8.2 Postback, ViewState
Presentation transcript:

11 ASP.NET Server Controls Beginning ASP.NET in C# and VB Chapter 4

22 Objectives You will be able to Use basic ASP.NET controls to design an ASP.NET Web Forms page. Process user inputs from ASP.NET controls in server side C# code. Understand the relationship between the ASP.NET code that you write in Visual Studio and the HTML that is received by the browser.

3 ASP.NET Controls Building blocks for a web page Normal HTML HTML Server Controls ASP.NET Server Controls ASP.NET AJAX Controls User/Custom controls

4 Normal HTML Can be included in an ASPX.NET page. Passed to browser as written. Usually not processed by server code. Server code can get user input values from normal HTML input controls.

5 HTML Server Controls Discussed in Chapter 4 of our textbook. Normal HTML plus “runat=server” Permit server code to modify the HTML. Mainly to support backward compatibility. Move existing HTML into an ASP.NET site. Not used in this class.

6 ASP.NET Server Controls Pseudo-HTML Include “asp:” prefix text Translated into real HTML before being sent to browser. WYSIWYG Designer in Visual Studio. Conveniently accessible to our server side code. May result in JavaScript script being sent to the browser and exectuted there. Used extensively in this course.

7 ASP.NET AJAX Server Controls Include Javascript code that runs on the browser. Permits more interactive user interface. Ignore for now. We will look at AJAX in some depth later in the course.

8 User Controls Controls created by the developer i.e. by us! Analogous to functions in software. Reusable bundles of code. Consist of existing controls and HTML Covered lightly later in the course.

99 Example A simple web application using ASPX controls matching the HTML form seen earlier.

10 Example Permits user to enter personal information. A typical real web app would write this information into a database. We will use a Visual Studio breakpoint to look at the user inputs as properties of ASPX control objects. Create a new empty web site called ASPX_Demo.

11 New Web Site

12 New Web Site in Visual Studio

13 Empty Web Site

14 Add New Page to the Website

15 Set Page Name to Demo.aspx Set file name.

16 Initial File Set Design View.

17 Demo.aspx in Design View View Toolbox. Position cursor in div box.

Demo.aspx in Design View 18

19 Design the Form Add “Standard” (ASPX) controls from the Toolbox to the design surface to design a form similar to the one seen earlier. Double click to add at cursor position Or drag and drop. Use space bar and new line to control position of elements. Check the Source view from time to time as you lay out the form.

20 Normal Flow In the browser, controls normally appear on the page like text in a word processing program. Left to right Top to bottom This is usually what you should do. HTML position attributes can force exceptions to normal flow.

21 Design the Form Give each input element a meaningful name. To put radio buttons into the same group give them the same value for their GroupName property.

22 Design the Form To specify values for the Classification Dropdown List, click on Items in its Properties. Click on the... icon for Items. Use numeric values, 0 – 5, for “Value”. Click OK when finished.

The Form in Design View 23

The Form in Source View 24

25 Title is a Property of the Document Set Title Select DOCUMENT

26 What We Wrote

27 What we wrote

28 File ASPX_Demo.aspx This text file holds the results of our design work. We could have written it with NotePad. The visual designer is just a more convenient way of writing.

29 Build and Run Click the "Play" button to view the page in your default browser.

30 Demo.aspx in Chrome

31 What the Browser Received Every aspx web forms page consists of exactly one HTML form. The action attribute specifies the URL to request when the user clicks the Submit button.

32 The TextBoxes Each ASPX Label became an HTML span. Each ASPX TextBox became an HTML input, type=“text”

33 The Radio Buttons Each asp:RadioButton became an HTML input, type=“radio”. ASPX “ID” became “id” in HTML. ASPX “GroupName” became “name” in HTML. ASPX “Text” became text in an HTML label.

34 Checkbox and Dropdown List The asp:CheckBox became an HTML input, type = “checkbox” The asp:DropDownList became an HTML select. Each asp.ListItem became an HTML option.

35 The Buttons Each asp:Button became an HTML input, type=“submit”.

36 Postback Clicking the Submit button will result in a postback. Changing other inputs does not. Users expect to be able to make changes to inputs before they click a button to submit them.

37 Code to Access Inputs We can write C# code to process inputs. The “code behind” file, Demo.aspx.cs Double click on Demo.aspx.cs in the Solution Explorer. Visual Studio opens the file in an editor window.

38 Demo.aspx.cs Can be deleted.

39 Inputs Input from each control is available as a property of a C# object corresponding to the control. Object name is the name that we gave the control in the ASPX page.

40 Code to Access Inputs using System; public partial class Demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { String Last_Name = tbLastName.Text; String First_Name = tbFirstName.Text; Boolean Unspecified = rbUnspecified.Checked; Boolean Male = rbMale.Checked; Boolean Female = rbFemale.Checked; Boolean CSE_Dept_Major = cbCseDeptMajor.Checked; String Classification = ddlClassification.SelectedValue; }

41 Looking at Input Values Set breakpoint at the end of the “if” block and examine the variables. Hover the mouse over a variable. Or rightclick and select QuickWatch.

42 Inputs Filled In

43 Stopped at Breakpoint Right click on a variable and select QuickWatch.

44 QuickWatch

45 QuickWatch Or just hover the cursor over a variable and see its value in a tooltip.

46 Looking at Input Values Where do these values come from? When a postback is done, the server (ASPX) instantiates objects corresponding to our ASPX controls. Initializes objects’ properties with values received from the browser. Our event handler code can refer to the control objects by name and access their properties.

47 Looking at Input Values The only data available to the server is that provided by the user in the HTML input elements. Name-Value pairs are included in the request by the browser. Appended to the URL for “get” Included in the request Message for “post”

48 Looking at Input Values The entire collection of name-value pairs is directly available to our code as the Form property of the Request object. Set up by IIS when it processes a page request. Would include values from plain HTML inputs if there were any. Let’s add some code to look at the inputs collection directly.

Looking at the Input Values using System; using System.Collections.Specialized; public partial class Demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { NameValueCollection inputs = Request.Form; String Last_Name = inputs["tbLastName"]; String First_Name = inputs["tbFirstName"]; String Gender = inputs["Gender"]; String CSE_Dept_Major = inputs["cbCSEDeptMajor"]; String Classification = inputs["ddlClassification"]; } 49 Note C# indexer syntax. Request.Form holds all inputs as Name-Value pairs

50 QuickWatch for an Input Value

51 The Gender Input Note that there is only one input for the gender RadioButton group. Name is the ASPX controls' GroupName Value is the ID of the checked radio button. Contrast to representation as object properties.

52 New Page on Browser Inputs are still set up. (Unlike.html)

53 What the Browser Received End of Section

54 Refinements We have seen the basic functionality of an ASPX web app. Next we will look at a number of refinements. Get a deeper understanding of ASPX controls. Get some tips about page design.

55 Setting the Focus No control has the focus when the form is first displayed. We have to click on the Last Name textbox in order to enter text. The app can set the focus programmatically using the Focus method of any input. This is implemented by a JavaScript script created by AXPX and sent to the browser along with the translated ASPX page.

56 Setting the Focus protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { NameValueCollection inputs = Request.Form; String Last_Name = inputs["tbLastName"]; String First_Name = inputs["tbLastName"]; String Gender = inputs["Gender"]; String CSE_Dept_Major = inputs["cbCseMajor"]; String Classification = inputs["ddlClassification"]; } else { tbLastName.Focus(); }

57 Advancing the Focus Users expect to move the focus to the next control by hitting tab. Currently this doesn’t work right. Controlled by TabIndex property of the controls. Go to control with next higher value. Skip over values of 0.

58 Controlling Tab Order Set TabIndex property of tbLastName and tbFirstName to 1 and 2. Leave others set to 0. Try it!

59 The Default Button We can specify a button to be automatically clicked if the user hits the Enter key. In source view, select just the opening tag. Set the DefaultButton property of the tag to btnSubmt.

60 The Default Button

61 The Default Button in Source View End of Section

62 AutoPostBack Most controls other than Buttons don’t cause an immediate postback. Changes result in events that can be handled when the browser posts the form back due to user clicking a button. The AutoPostBack property causes an immediate postback when the control is changed. Note: This is usually NOT what we want.

63 Enable/Disable Most controls have an Enabled property. Set to false to disable the control. Set to true to enable the control. Let’s disable the Submit button until the user has entered something into both name TextBoxes.

64 Submit Button Disabled

65 TextChanged Event The TextBoxes can fire an event when the user changes anything in them. TextChanged Add event handers for the two TextBoxes. When user has entered text into both boxes, enable Submit.

66 Adding an Event Handler In Design View, double click inside each TextBox. Visual Studio adds an event handler for the TextChanged event.

67 TextChanged Event Handler

68 The Event Handler Add code to enable the Submit button if both textboxes have some text.

69 The Event Handler Do the same for tbFirstName.

70 Initial Page in Chrome Submit button is disabled.

71 Page in Chrome Fill in information. Submit button is still disabled! What’s going on here? Why didn’t our event handler work?

72 No TextChanged Event The TextChanged event will not fire on the server until a postback is done. The postback will not be done until the Submit button is clicked. But the Submit button is disabled. We need AutoPostBack on the TextBoxes.

73 Set AutoPostBack Select tbLastName and view its properties. Set AutoPostBack to true. Do the same for tbFirstName.

74 Try Again! Submit button is enabled.

75 Reset the Form Let’s clear the form after a successful registration. Provide feedback to the user. Leave the form ready for another user.

76 Reset the Form

77 Try it! Enter last name and press tab.

78 After Tab Now what’s wrong? Last name disappears!

79 AutoPostBack Leaving tbLastName caused a postback. The postback resulted in a call to Reset_Form. We need to clear the form only when the postback is due to click on Submit. Remove call to Reset_Form from Page_Load. Double click on the Submit button in the Design view to add an event handler for the click event. Call Reset_Form.

80 Submit Click Event Handler Now the app works as expected.

81 Assignment Do the examples from this class for yourself if you did not do them in class. Read Chapter 4 of textbook Beginning ASP.NET in C# and VB End of Presentation