Working with ASP.NET Server Controls

Slides:



Advertisements
Similar presentations
Unit 02. ASP.NET Introduction HTML & Server controls Postbacks Page Lifecycle.
Advertisements

11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
Asp.NET Core Server Controls. Slide 2 Lecture Overview Understanding the types of ASP.NET controls HTML controls ASP.NET (Web) controls.
Web-Based Applications
Web Development Using ASP.NET CA – 240 Kashif Jalal Welcome to week – 3-1 of…
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
Chapter 10 Form Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D 1.
11 ASP.NET Controls Beginning ASP.NET 4.0 in C# 2010 Chapter 6.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
ASP.NET Programming with C# and SQL Server First Edition
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
Overview This presentation covers the initial overview of the different server controls. 2.
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.
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.
1 Presented by Bikash Shrestha. 2 What is ASP.NET or ASP+? ASP.NET/ASP+ is a programming framework built on the common language runtime that can be used.
HTML, GUI, ASP.NET Rina Zviel-Girshin Lecture 2
.Net is a collection of libraries, templates and services designed to make programming applications of all kinds, easier, more flexible (multi platform),
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Beginning Web Site Development Module 1 – Dynamic Web Site Development Fundamentals of building dynamic Web sites with ASP.NET 2.0 and C# Version.
ASP.Net Web Applications. Characteristics of a typical data driven web application Web Server HTML Graphics Active-X Java Applets HTTP Request ADO / JDBC.
Web Development Using ASP.NET CA – 240 Kashif Jalal Welcome to week – 4-1 of…
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
ASP.NET Web Server Controls Basic Web Server Controls.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
Chapter 4 – Working with ASP.NET Controls Dr. Stephanos Mavromoustakos.
ASP.NET Controls. Slide 2 Lecture Overview Identify the types of controls supported by ASP.NET and the differences between them.
ASP.NET.. ASP.NET Environment ASP.NET is Microsoft's programming framework that enables the development of Web applications and services. It is an easy.
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
Week 9 - Form Basics Key Concepts 1. 1.Describe common uses of forms on web pages 2.Create forms on web pages using the form, input, textarea, and select.
Christopher M. Pascucci Basic Structural Concepts of.NET Managing State & Scope.
Things like Textboxes, Lables, ‘n’at. ASPX page is not HTML Controls are rendered into markup that a browser can understand Some controls are rendered.
Module 4: Creating a Microsoft ASP.NET Web Form. Overview Creating Web Forms Using Server Controls.
Module 1: Working with ASP.NET. Overview Introducing ASP.NET Creating Web Forms Adding ASP.NET Code to a Page Handling Page Events Discussion: ASP vs.
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.
ASP.NET 4 Unleashed Chapter 1. .aspx page: contains C# script and HTML code including tags. Listing 1.1 FirstPage.aspx.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Asp.NET Core Server Controls. Slide 2 Lecture Overview Understanding the types of ASP.NET controls HTML controls ASP.NET (Web) controls.
11 ASP.NET Server Controls Beginning ASP.NET in C# and VB Chapter 4.
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
PostBack  When an initial request for a page (a Web Form) is received by ASP.NET, it locates and loads the requested Web Form (and if necessary compiles.
1 Introducing Web Developer Tools Rapid application development tools ASP.NET-compatible web editors –Visual Studio.NET Professional Edition –Visual Studio.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
E-Commerce: Introduction to ASP.Net Application Architecture 1 Dr. Lawrence West, Management Dept., University of Central Florida Topics.
2440: 141 Web Site Administration Web Forms Instructor: Joseph Nattey.
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
Jim Fawcett CSE686 – Internet Programming Spring 2014
Computing with C# and the .NET Framework
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
ASP.NET Forms.
ASP.NET Web Controls.
Internet Programming Chapter 9: State Management in ASP.NET
Chapter 8 User Controls.
Introduction to Client-Server Programming
Basic XHTML Tables XHTML tables—a frequently used feature that organizes data into rows and columns. Tables are defined with the table element. Table.
Unit 27 - Web Server Scripting
Client side & Server side scripting
State management & Master Pages in asp.net
Forms, cont’d.
Web Development & Design Foundations with H T M L 5
Static and Dynamic Web Pages
Teaching slides Chapter 6.
ASP.NET.
Module 05: Building ASP .NET Applications
Web Development Using ASP .NET
5/6/2019 Session 8.2 Postback, ViewState
Programming with Microsoft Visual Basic 2008 Fourth Edition
Presentation transcript:

Working with ASP.NET Server Controls Chapter 4 Working with ASP.NET Server Controls

Objectives Learn what ASP.NET Controls are. How the ASP.NET runtime processes the server controls on our page. Common behavior shared among different server controls. How server controls are able to maintain their state across postbacks.

ASP.NET Server Controls ASP.net controls are the powerful workhorses behind the framework. They help provide user interaction functionality by creating the UI components on the markup and the programmability on the code behind (source code) The previous framework, classic ASP combined code and presentation <input type="text" value="Hello World, the time is <%=Time()%>" /> .NET Server controls are more elegant and help maintain the code <asp:TextBox Id=“txtHello” runat=“server” /> txtHello.Text = “Hello World, the time is “ + DateTime.Now.TimeOfDay.ToString()

ASP.NET Server Controls Fundamentally, server controls operate on the server side, meaning that after the user has made a specific request from their browser and the url is found the web server which processes the page will now execute that server control. With .NET Server controls, the ASP.NET runtime engine which is part of the web server (usually I.I.S.) processes all the server controls which live inside the web server. After processing, these controls emit client side HTML code that gets sent to the browser.

ASP.NET Server Controls The following is the format: <asp:TypeOfControl ID="ControlName" runat="server" /> Where: TypeOfControl is the control (Button, Label, etc.). ID is a unique identifier to user to refer to the control on the page and the runat attribute tells the ASP.NET runtime to process that control. You can also close the tag with the long version, as seen here <asp:TextBox Id="Message" runat="server"></asp:TextBox>

ASP.NET Server Controls As we mentioned, the power of these server controls is that you can program against them. For example, Given the following Textbox, we can change the message with the following  Message.Text = "Hello World, the time is " + DateTime.Now.TimeOfDay.ToString(); With this flexibility, we can drive the design from the markup and the programmability from the code block. Note that the markup is not case sensitive, but the code block is.

Postback and Programmability Support we have a label, textbox and button like so: The label has ID Result and the Textbox has an ID of Yourname. In the design view, we can double click on the button and it generates some code for us known as an event handler. The event handler will create a piece of code to handle our click.

Postback and Programmability protected void SubmitButton_Click(object sender, EventArgs e) {      Result.Text = "Your name is " + YourName.Text; } Once this button is actually clicked from a browser, it creates what is known as a postback which sends the information from all the .net controls to the server. A postback is a call from the client to the server. The SubmitButton _Click method is executed which adds the text to the Result label.

Postback and Programmability The outcome is that our Label control changes with the name that was specified in the textbox. The HTML that is sent from the server side back to the client, is the following: <span id="Result">Your name is Tom</span>

A Closer look at ASP.NET Server Controls You can drag and drop controls from the toolbox in markup view or you can type the control directly in markup view. To make it easier, when you click on a control you can always use the properties grid to change. Common properties for all controls ID: Unique identifier user by the server runtime for the page. runat: Tells the runtime engine to process this control clientID: ID that gets sent back o the browser/ HTML ID.

Other Properties

Other Properties

Maintain readability in your controls Move the styling to CSS if possible if your expect the control to have a lot of the properties OK  Better 

Type of Controls Simple Controls: TextBox, Button, Label, Hyperlink, RadioButton, Checkbox List Controls, Presented as a list in the browser: ListBox, DropdwonList, CheckBoxList, RadioButtonList, BulletedList Container Controls, used to group related content and controls: Panel, Placeholder, Multiview, Wizard Other Standard Controls: LinkButton, ImageButton, Image, ImageMap, Calendar, FileUpload, Literal, Hiddenfield (very useful), Table

Type Of Controls Data Controls: Used to access databases, XML or objects. Validation Controls: Client/Server side controls that provide validation. Navigation Controls: Treeview, Menu, Sitemappath Login Controls: Creates secure login and related functionalities Ajax extensions: Controls enable you to retrieve data from the server side with doing a full postback (sending the entire page to the server). Used for “flicker-free” interaction.

HTML Controls and when to use? There are the client side HTML controls. You can access their properties on the server side by adding runat=“server” Normally, you will need to leverage the functionality of a .NET control at development time or in the future, so in that case you would use a .NET control. However, if you are absolutely sure that the control will not programmed against on the server side, you can use the HTML controls. If unsure, use the .NET control.

ASP.NET State Engine Consider that adding what happens when you have a label, you change its value and click a button which causes a postback. When the request comes back from the postback the label still has the value which was entered. Why? Given what we know about the HTTP protocol and its stateless nature, something should be occurring to maintain the state. The value in the label is maintained by the ASP.NET state engine, a feature that is deeply integrated into the ASP.NET run time. It enables controls to maintain their state across postbacks, so their values and settings remain available after every postback of the page.

ASP.NET State Engine Consider the form control HTML form control <form>. <form method="post" action="State.aspx" id="form1"> ... </form> This form can post information one of two ways: POST: All data from the form is added to the body of request. GET: All data is appended to the actual address of a request. The form controls encapsulates controls to submit back to the server when a button control is clicked.

ASP.NET State Engine Now consider an ASP Web Form, which has the runat=“server”. It always uses the POST method to add all the controls to the body of the request. Every .NET Page contains a form element by default. The Form normally posts back to itself. (Cross page posting is an option) The form must be able to pass the value back to the server. We can find this in a hidden control known as the VIEWSTATE. <form method="post" action="State.aspx" id="form1"> ... <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="IXcrUZ51B9YmtdoSL9csn2+VrYx5oW32kAw0oRXGsf3F0/W0l6/upieH7Nht1f hyr99U0IRRKmjvYk4FdH5E9ZRucaja0xPkwCyRoNBI3KkidqR5eAVX86D qOfEl584eSB0ff3IF4o3Y+ZqD7qZp3A==" /> </div>

ASP.NET State Engine To protect the information stored in this field, ASP.NET has serialized the page state in the preceding string. If you were able to look inside the value of the fields, you’d find a value for the label that you changed. Not all controls rely on the Viewstate however. Some send the values in the html they send. Examples: TextBox, CheckBox, RadioButton, DropwDownList <input name="TextBox1" type="text" value="Initial Text" id="TextBox1" /> ASP.NET can just get the value from the “value” attribute. Adding it to the Viewstate would be unnecessary. Controls have an attribute “EnableViewState” which you can set to false so their values don’t get included in the ViewState.

ASP.NET State Engine Another item saved in the VIEWSTATE is a control state, which keeps track of data they need to operate correctly. This is not something that you can turn of.. So remember by default, ASP.NET controls track their state in the ViewState, which is serialized into a value for a hidden control. <form method="post" action="State.aspx" id="form1"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="IXcrUZ51B9YmtdoSL9csn2+VrYx5oW32kAw0oRXGsf3F0/W0l6/upieH7Nht1f hyr99U0IRRKmjvYk4FdH5E9ZRucaja0xPkwCyRoNBI3KkidqR5eAVX86D qOfEl584eSB0ff3IF4o3Y+ZqD7qZp3A==" /> </div>

ViewState and Performance The more controls get added to the Viewstate, the heavier its footprint on the page, so consider disabling the Viewstate when not used. Viewstate can be turned off at: Website Level: Modify the web config under the <system.web> section <pages enableViewState="false"> At the page level on the page directive <%@ Page Language="VB" AutoEventWireup="False" CodeFile="State.aspx.vb" Inherits="Demos _ State" EnableViewState="False" %> At the individual control level with EnableViewState = “False” To turn of by default at page level, but respect individual controls: <%@ Page Language="C#" … EnableViewState="True" ViewStateMode="Disabled" %>

Try it Out Page 103 Page 110 Page 115 Page 117 Page 125

Summary In this chapter we covered: ASP.NET Server Controls, what they are and what are their components. How a postback works. The different type individual .NET controls ASP.NET State Engine and the hidden ViewState control. How to disable ViewState at various levels.