+ All Categories
Home > Documents > Intro to ASP.NET and Web Forms

Intro to ASP.NET and Web Forms

Date post: 04-Apr-2018
Category:
Upload: mcguled
View: 229 times
Download: 0 times
Share this document with a friend

of 107

Transcript
  • 7/29/2019 Intro to ASP.NET and Web Forms

    1/107

    Introduction to ASP.NET

    and Web Forms

    Mark Sapossnek

    CS 594

    Computer Science Department

    Metropolitan CollegeBoston University

  • 7/29/2019 Intro to ASP.NET and Web Forms

    2/107

    Prerequisites

    This module assumes that you understand the

    fundamentals of

    C# programming ADO.NET

    A background in web development (HTML,

    JavaScript, DHTML, CGI, Active Server Pages)

    would be helpful, but is not required

  • 7/29/2019 Intro to ASP.NET and Web Forms

    3/107

    Learning Objectives

    What is ASP.NET; why it was developed

    ASP.NET features

    Programming Web Forms

  • 7/29/2019 Intro to ASP.NET and Web Forms

    4/107

    Agenda

    Background

    ASP.NET Overview

    Programming Model Programming Basics

    Server Controls

    Data Binding Conclusion

  • 7/29/2019 Intro to ASP.NET and Web Forms

    5/107

    BackgroundWeb Architecture

    Web Server

    PC/Mac/Unix/...

    + BrowserClient

    Server

    Request:

    http://www.digimon.com/default.asp

    Response:.

    Network HTTP, TCP/IP

  • 7/29/2019 Intro to ASP.NET and Web Forms

    6/107

    BackgroundWeb Development Technologies

    Client-side technologies

    HTML, DHTML, JavaScript

    Server-side technologies ASP (Active Server Pages)

    ASP.NET is the next generation of ASP

  • 7/29/2019 Intro to ASP.NET and Web Forms

    7/107

    BackgroundWhat is ASP?

    Server-side programming technology

    Consists of static HTML interspersed with script

    ASP intrinsic objects (Request, Response,Server, Application, Session) provide services

    Commonly uses ADO to interact with databases

    Application and session variables Application and session begin/end events

    ASP manages threads, database connections, ...

  • 7/29/2019 Intro to ASP.NET and Web Forms

    8/107

    BackgroundWhat is ASP?

    HTTP request(form data, HTTP

    header data)

    HTTP responseHTML, XML

    ASP page

    (static HTML,server-side logic)

  • 7/29/2019 Intro to ASP.NET and Web Forms

    9/107

    BackgroundDemo: HelloWorld.asp

  • 7/29/2019 Intro to ASP.NET and Web Forms

    10/107

    BackgroundASP Successes

    Simple procedural programming model

    Access to COM components

    ActiveX Data Objects (ADO) File System Object

    Custom components

    Script-based: no compiling, just edit, save & run

    VBScript, JScript leverages existing skills

    Support for multiple scripting languages

    ASP has been very popular

  • 7/29/2019 Intro to ASP.NET and Web Forms

    11/107

    BackgroundASP Challenges

    Coding overhead (too much code)

    Everything requires writing code!

    Code readability (too complex; code and UI intermingled)

    Maintaining page state requires more code

    Reuse is difficult

    Supporting many types of browsers is difficult

    Deployment issues (e.g. DLL locking)

    Session state scalability and availability

    Limited support for caching, tracing, debugging, etc.

    Performance and safety limitations of script

  • 7/29/2019 Intro to ASP.NET and Web Forms

    12/107

    Agenda

    Background

    ASP.NET Overview

    Programming Model Programming Basics

    Server Controls

    Data Binding

    Conclusion

  • 7/29/2019 Intro to ASP.NET and Web Forms

    13/107

    ASP.NET Overview

    ASP.NET provides services to allow the

    creation, deployment, and execution of

    Web Applications and Web Services

    Like ASP, ASP.NET is a server-side technology

    Web Applications are built using Web Forms

    Web Forms are designed to make building

    web-based applications as easy as building

    Visual Basic applications

  • 7/29/2019 Intro to ASP.NET and Web Forms

    14/107

    ASP.NET OverviewGoals

    Keep the good parts of ASP and improve the rest

    Simplify: less code, easier to create and maintain

    Multiple, compiled languages

    Fast

    Scalable

    Manageable

    Available Customizable and extensible

    Secure

    Tool support

  • 7/29/2019 Intro to ASP.NET and Web Forms

    15/107

    ASP.NET OverviewKey Features

    Web Forms

    Web Services

    Built on .NET Framework Simple programming

    model

    Maintains page state

    Multibrowser support XCOPY deployment

    XML configuration

    Complete object model

    Session management

    Caching

    Debugging Extensibility

    Separation of code and UI

    Security

    ASPX, ASP side by side Simplified form validation

    Cookieless sessions

  • 7/29/2019 Intro to ASP.NET and Web Forms

    16/107

    ASP.NET OverviewDemo: HelloWorld.aspx

  • 7/29/2019 Intro to ASP.NET and Web Forms

    17/107

    ASP.NET OverviewArchitecture

    ASP.NET is built upon

    .NET Framework

    Internet Information Server (IIS)

  • 7/29/2019 Intro to ASP.NET and Web Forms

    18/107

    ASP.NET OverviewArchitecture

    Internet Information Server (IIS)

    IIS MMC Snap-In (Internet Services Manager)

    Tool to manage IIS

    Virtual Directories

    Provides a mapping between URL and file path

    E.g., on my machine the URL:

    http://localhost/CS594

    maps to the file path:C:\_CS594Fall2001

  • 7/29/2019 Intro to ASP.NET and Web Forms

    19/107

    Common Language Specification

    Common Language Runtime

    VB C++ C#

    ASP.NET: Web Servicesand Web Forms

    JScript

    WindowsForms

    Base Classes

    ADO.NET: Data and XML

    Vis

    ualStudio.NET

    ASP.NET OverviewArchitecture

  • 7/29/2019 Intro to ASP.NET and Web Forms

    20/107

    Agenda

    Background

    ASP.NET Overview

    Programming Model Programming Basics

    Server Controls

    Data Binding

    Conclusion

  • 7/29/2019 Intro to ASP.NET and Web Forms

    21/107

    Programming ModelControls and Events

    Server-side programming model

    Based on controls and events

    Just like Visual Basic Not data in, HTML out

    Higher level of abstraction than ASP

    Requires less code

    More modular, readable, and maintainable

  • 7/29/2019 Intro to ASP.NET and Web Forms

    22/107

    Programming ModelControls and Events

    Button

    List

    Text

    Browser ASP.NET

    Button code...

    List code...

    Text code

    ...

    Event handlers

  • 7/29/2019 Intro to ASP.NET and Web Forms

    23/107

    Programming ModelASP.NET Object Model

    User code executes on the web server in

    page or control event handlers

    Controls are objects, available inserver-side code

    Derived from System.Web.UI.Control

    The web page is an object too

    Derived from System.Web.UI.Page which is adescendant ofSystem.Web.UI.Control

    A page can have methods, properties, etc.

  • 7/29/2019 Intro to ASP.NET and Web Forms

    24/107

    Programming ModelPostbacks

    A postback occurs when a page generates an

    HTML form whose values are posted back

    to the same page

    A common technique for handling form data

    In ASP and other server-side technologies the

    state of the page is lost upon postback...

    Unless you explicitly write code to maintain state

    This is tedious, bulky and error-prone

  • 7/29/2019 Intro to ASP.NET and Web Forms

    25/107

    Programming ModelPostbacks Maintain State

    By default, ASP.NET maintains the state of all

    server-side controls during a postback

    Can use method= post ormethod= get

    Server-side control objects are automatically

    populated during postback

    No state stored on server

    Works with all browsers

  • 7/29/2019 Intro to ASP.NET and Web Forms

    26/107

    Programming ModelServer-side Controls

    Multiple sources of controls

    Built-in

    3rd party

    User-defined

    Controls range in complexity and power: button,

    text, drop down, calendar, data grid, ad rotator,

    validation Can be populated via data binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    27/107

    Programming ModelAutomatic Browser Compatibility

    Controls can provide automatic browser

    compatibility

    Can target UpLevel orDownLevel browsers UpLevel browsers support additional functionality,

    such as JavaScript and DHTML

    DownLevel browsers support HTML 3.2

  • 7/29/2019 Intro to ASP.NET and Web Forms

    28/107

    Programming ModelAutomatic Browser Compatibility

    Button code...

    Menu code...

    Text code...

    Event handlers

    Button

    Menu

    Text

    IE 4

    Button

    Menu

    Text

    Netscape

    Button

    Menu

    Text

    IE 5.5

    Button

    Menu

    Text

    IE 6

    ASP.NET

    Button Control

    Menu Control

    Text Control

    ...

  • 7/29/2019 Intro to ASP.NET and Web Forms

    29/107

    Programming ModelCode-behind pages

    Two styles of creating ASP.NET pages

    Controls and code in .aspx file

    Controls in .aspx file, code in code-behind page

    Supported in Visual Studio.NET

    Code-behind pages allow you to separate the

    user interface design from the code

    Allows programmers and designers to workindependently

  • 7/29/2019 Intro to ASP.NET and Web Forms

    30/107

    Programming ModelAutomatic Compilation

    Just edit the code and hit the page

    ASP.NET will automatically compile the code

    into an assembly Compiled code is cached in the CLR

    Assembly Cache

    Subsequent page hits use compiled assembly

    If the text of the page changes then the code

    is recompiled

    Works just like ASP: edit, save and run

  • 7/29/2019 Intro to ASP.NET and Web Forms

    31/107

    Programming ModelAutomatic Compilation

  • 7/29/2019 Intro to ASP.NET and Web Forms

    32/107

    Agenda

    Background

    ASP.NET Overview

    Programming Model Programming Basics

    Server Controls

    Data Binding

    Conclusion

  • 7/29/2019 Intro to ASP.NET and Web Forms

    33/107

    Programming BasicsPage Syntax

    The most basic page is just static text Any HTML page can be renamed .aspx

    Pages may contain: Directives:

    Server controls:

    Code blocks:

    Data bind expressions:

    Server side comments:

    Render code: and Use is discouraged; use with code in

    event handlers instead

  • 7/29/2019 Intro to ASP.NET and Web Forms

    34/107

    Programming BasicsThe Page Directive

    Lets you specify page-specific attributes, e.g.

    AspCompat: Compatibility with ASP

    Buffer: Controls page output buffering

    CodePage: Code page for this .aspx page ContentType: MIME type of the response

    ErrorPage: URL if unhandled error occurs

    Inherits: Base class of Page object

    Language: Programming language

    Trace: Enables tracing for this page

    Transaction: COM+ transaction setting

    Only one page directive per .aspx file

  • 7/29/2019 Intro to ASP.NET and Web Forms

    35/107

    Programming BasicsServer Control Syntax

    Controls are declared as HTML tags with

    runat=server attribute

    Tag identifies which type of control to create

    Control is implemented as an ASP.NET class

    The id attribute provides programmatic identifier

    It names the instance available during postback

    Just like Dynamic HTML

  • 7/29/2019 Intro to ASP.NET and Web Forms

    36/107

    Programming BasicsServer Control Properties

    Tag attributes map to control properties

    Tags and attributes are case-insensitive

    Control properties can be set programmatically

  • 7/29/2019 Intro to ASP.NET and Web Forms

    37/107

    Programming BasicsMaintaining State

    By default. controls maintain their state acrossmultiple postback requests Implemented using a hidden HTML field:

    __VIEWSTATE Works for controls with input data (e.g. TextBox,CheckBox), non-input controls (e.g. Label,DataGrid), and hybrids (e.g. DropDownList,ListBox)

    Can be disabled per control or entire page Set EnableViewState=false

    Lets you minimize size of__VIEWSTATE

  • 7/29/2019 Intro to ASP.NET and Web Forms

    38/107

    Programming BasicsMaintaining State

    Demo: MaintainingState.asp,

    MaintainingState.aspx

  • 7/29/2019 Intro to ASP.NET and Web Forms

    39/107

    Programming BasicsServer Code Blocks

    Server code lives in a script block marked

    runat=server

    Script blocks can contain

    Variables, methods, event handlers, properties

    They become members of a custom Page object

  • 7/29/2019 Intro to ASP.NET and Web Forms

    40/107

    Programming BasicsPage Events

    Pages are structured using events

    Enables clean code organization

    Avoids the Monster IF statement

    Less complex than ASP pages

    Code can respond to page events

    e.g. Page_Load, Page_Unload

    Code can respond to control events Button1_Click

    Textbox1_Changed

  • 7/29/2019 Intro to ASP.NET and Web Forms

    41/107

    Programming BasicsPage Event Lifecycle

    Initialize

    Restore Control State

    Save Control State

    Render

    1. Change Events

    2. Action Events

    Control EventsLoad Page

    Unload Page

  • 7/29/2019 Intro to ASP.NET and Web Forms

    42/107

    Programming BasicsPage Loading

    Page_Load fires at beginning of request aftercontrols are initialized

    Input control values already populated

  • 7/29/2019 Intro to ASP.NET and Web Forms

    43/107

    Programming BasicsPage Loading

    Page_Load fires on every request

    Use Page.IsPostBack to execute conditional logic

    If a Page/Control is maintaining state then need onlyinitialize it when IsPostBack is false

  • 7/29/2019 Intro to ASP.NET and Web Forms

    44/107

    Programming BasicsServer Control Events

    Change Events By default, these execute only on next action event

    E.g. OnTextChanged, OnCheckedChanged

    Change events fire in random order

    Action Events Cause an immediate postback to server

    E.g. OnClick

    Works with any browser No client script required, no applets,

    no ActiveX Controls!

  • 7/29/2019 Intro to ASP.NET and Web Forms

    45/107

    Programming BasicsWiring Up Control Events

    Control event handlers are identified on the tag

    Event handler code

  • 7/29/2019 Intro to ASP.NET and Web Forms

    46/107

    Programming BasicsEvent Arguments

    Events pass two arguments:

    The sender, declared as type object

    Usually the object representing the control that generated

    the event

    Allows you to use the same event handler for

    multiple controls

    Arguments, declared as type EventArgs

    Provides additional data specific to the event EventArgs itself contains no data; a class derived fromEventArgswill be passed

  • 7/29/2019 Intro to ASP.NET and Web Forms

    47/107

    Programming BasicsPage Unloading

    Page_Unload fires after the page is rendered

    Dont try to add to output

    Useful for logging and clean up

  • 7/29/2019 Intro to ASP.NET and Web Forms

    48/107

    Programming BasicsDirective

    Adds code namespace reference to page

    Avoids having to fully qualify .NET types and

    class names

    Equivalent to the C# using directive

  • 7/29/2019 Intro to ASP.NET and Web Forms

    49/107

    Programming BasicsClass

    The Page object is always available whenhandling server-side events

    Provides a large set of useful properties and

    methods, including: Application, Cache, Controls,EnableViewState, EnableViewStateMac,ErrorPage, IsPostBack, IsValid, Request,

    Response, Server, Session, Trace, User,Validators

    DataBind(), LoadControl(), MapPath(),Validate()

  • 7/29/2019 Intro to ASP.NET and Web Forms

    50/107

    Agenda

    Background

    ASP.NET Overview

    Programming Model

    Programming Basics

    Server Controls

    Data Binding Conclusion

  • 7/29/2019 Intro to ASP.NET and Web Forms

    51/107

    Server Controls

    ASP.NET ships with ~50 built-in controls

    Organized into logical families

    HTML controls Controls / properties map 1:1 with HTML

    Web controls

    Richer functionality

    More consistent object model

  • 7/29/2019 Intro to ASP.NET and Web Forms

    52/107

    Server ControlsHTML Controls

    Work well with existing HTML designers

    Properties map 1:1 with HTMLtable.bgcolor ="red;

    Can specify client-side event handlers

    Good when quickly converting existing pages

    Derived fromSystem.Web.UI.HtmlControls.HtmlControl

    Supported controls have custom class,

    others derive from HtmlGenericControl

  • 7/29/2019 Intro to ASP.NET and Web Forms

    53/107

    Server ControlsHTML Controls

    Supported controls

  • 7/29/2019 Intro to ASP.NET and Web Forms

    54/107

    Server ControlsHTML Controls

    Demo 1: HTMLControls1.aspx

    Basic page lifecycle with HTML Controls

    Demo 2: HTMLControls2.aspx More HTML Controls

  • 7/29/2019 Intro to ASP.NET and Web Forms

    55/107

    Server ControlsHTML Controls

    Can use controls two ways:

    Handle everything in action events (e.g. button click)

    Event code will read the values of other controls (e.g. text,

    check boxes, radio buttons, select lists)

    Handle change events as well as action events

  • 7/29/2019 Intro to ASP.NET and Web Forms

    56/107

    Server ControlsWeb Controls

    Consistent object model

    Richer functionality

    E.g. AutoPostBack, additional methods

    Automatic uplevel/downlevel support

    E.g. validation controls

    Strongly-typed; no generic control

    Enables better compiler type checking

  • 7/29/2019 Intro to ASP.NET and Web Forms

    57/107

    Server ControlsWeb Controls

    Web controls appear in HTML markup as

    namespaced tags

    Web controls have an asp: prefix

    Defined in the System.Web.UI.WebControlsnamespace

    This namespace is automatically mapped to the

    asp: prefix

  • 7/29/2019 Intro to ASP.NET and Web Forms

    58/107

    Server ControlsWeb Controls

    Web Controls provide extensive properties to

    control display and format, e.g.

    Font

    BackColor, ForeColor

    BorderColor, BorderStyle, BorderWidth

    Style, CssClass

    Height, Width Visible, Enabled

  • 7/29/2019 Intro to ASP.NET and Web Forms

    59/107

    Server ControlsWeb Controls

    Four types of Web Controls

    Intrinsic controls

    List controls

    Rich controls

    Validation controls

  • 7/29/2019 Intro to ASP.NET and Web Forms

    60/107

    Server ControlsIntrinisic Controls

    Correspond to HTML controls

    Supported controls

  • 7/29/2019 Intro to ASP.NET and Web Forms

    61/107

    Server ControlsIntrinisic Controls

    TextBox, ListControl, CheckBox and theirsubclasses dont automatically do a postback

    when their controls are changed

    Specify AutoPostBack=true to make changeevents cause a postback

  • 7/29/2019 Intro to ASP.NET and Web Forms

    62/107

    Server ControlsList Controls

    Controls that handle repetition

    Supported controls

  • 7/29/2019 Intro to ASP.NET and Web Forms

    63/107

    Server ControlsList Controls

    Repeater, DataList and DataGrid controls

    Powerful, customizable list controls

    Expose templates for customization

    Can contain other controls

    Provide event bubbling through theirOnItemCommand event

    More about these controls and templates later

  • 7/29/2019 Intro to ASP.NET and Web Forms

    64/107

    Server Controls&

    Provides a collection of check box or

    radio button controls

    Can be populated via data binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    65/107

    Server ControlsIntrinisic & Simple List Controls

    Demo 1: WebControls1.aspx

    Assorted intrinsic and list controls

    Demo 2: WebControls2.aspx

    Same controls with AutoPostBack

  • 7/29/2019 Intro to ASP.NET and Web Forms

    66/107

    Server ControlsRich Controls

    Custom controls with rich functionality

    Supported Controls

    More will be added

    3rd party controls are coming

    Demo: RichControls1.aspx

  • 7/29/2019 Intro to ASP.NET and Web Forms

    67/107

    Server ControlsValidation Controls

    Rich, declarative validation

    Validation declared separately from input control

    Extensible validation framework

    Supports validation on client and server

    Automatically detects uplevel clients

    Avoids roundtrips for uplevel clients

    Server-side validation is always done

    Prevents users from spoofing Web Forms

  • 7/29/2019 Intro to ASP.NET and Web Forms

    68/107

    Server ControlsValidation Controls

    Ensures that a value is entered

    Checks if value is within minimum and maximum values

    Compares value against constant, another control or data type

    Tests if value matches a predefined pattern

    Lets you create custom client- or server-side validation function

    Displays list of validation errors in one place

    S C l

  • 7/29/2019 Intro to ASP.NET and Web Forms

    69/107

    Server ControlsValidation Controls

    Validation controls are derived from

    System.Web.UI.WebControls.BaseValidator,

    which is derived from the Label control

    Validation controls contain text which is displayed

    only if validation fails

    Text property is displayed at control location

    ErrorMessage is displayed in summary

    S C l

  • 7/29/2019 Intro to ASP.NET and Web Forms

    70/107

    Server ControlsValidation Controls

    Validation controls are associated with theirtarget control using the ControlToValidateproperty

    Can create multiple validation controls with thesame target control

    S C l

  • 7/29/2019 Intro to ASP.NET and Web Forms

    71/107

    Server ControlsValidation Controls

    Page.IsValid indicates if all validationcontrols on the page succeed

    S C l

  • 7/29/2019 Intro to ASP.NET and Web Forms

    72/107

    Server ControlsValidation Controls

    Display property controls layout

    Static: fixed layout, display wont change if invalid

    Dynamic: dynamic layout

    None: no display; can still use ValidationSummaryand Page.IsValid

    Type property specifies expected data type:

    Currency, Date, Double, Integer, String

    S C t l

  • 7/29/2019 Intro to ASP.NET and Web Forms

    73/107

    Server ControlsValidation Controls

    Can force down-level option

    Only server-side validation

    S C t l

  • 7/29/2019 Intro to ASP.NET and Web Forms

    74/107

    Server ControlsValidation Controls

    Demo: ValidationControls1.aspx

    Demonstrates each type of validation control

  • 7/29/2019 Intro to ASP.NET and Web Forms

    75/107

    Agenda

    Background

    ASP.NET Overview

    Programming Model Programming Basics

    Server Controls

    Data Binding

    Conclusion

    D t Bi di

  • 7/29/2019 Intro to ASP.NET and Web Forms

    76/107

    Data BindingHow to Populate Server Controls?

    Specify the data in the controls tags

    Not dynamic: cant get data from a database

    Write code that uses the controls object model

    This is okay if you need to populate a simple value or

    list, but quickly gets too complicated for populating

    sophisticated displays

    Data binding Create an object that holds the data

    (DataSet, Array, string, int, etc.)

    Associate that object with the control

    D t Bi di

  • 7/29/2019 Intro to ASP.NET and Web Forms

    77/107

    Data BindingWhat Is It?

    Provides a single simple yet powerful way to

    populate Web Form controls with data

    Enables clean separation of code from UI

    Supports binding to any data source

    Properties, expressions, method calls

    Collections (Array, Hashtable, etc.)

    DataSet, DataTable, DataView, DataReader XML

    One way snapshot model

    Requires code to reapply to data model

    D t Bi di

  • 7/29/2019 Intro to ASP.NET and Web Forms

    78/107

    Data BindingWhat Is It?

    Allows you to specify an expression

    When the DataBind method of the control iscalled, the expression is evaluated and bound

    DataBind for a single control (and subcontrols)

    Page.DataBind binds all controls on a page

    Works for scalars, e.g. Label control

    Works for lists, e.g. DropDown control,ListBox control, etc.

    Enables the use of templates

    D t Bi di

  • 7/29/2019 Intro to ASP.NET and Web Forms

    79/107

    Data BindingScalar Expressions

    Data binding expression:

    Expression is evaluated when DataBind()is called

    D t Bi di

  • 7/29/2019 Intro to ASP.NET and Web Forms

    80/107

    Data BindingScalar Expressions

    Demo: DataBinding1.aspx

    Data binding to simple, scalar expressions

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    81/107

    Data BindingSimple Lists

    Data binding a list creates a user interfaceelement for each item in the list

    Each item contains text (displayed to user) and

    an optional value (not displayed) The simple list controls:

    Single or multiple select

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    82/107

    Data BindingSimple Lists

    Steps to data bind a list control

    Declare the list control

    Optionally set DataValueField

    and DataTextField Set its DataSource

    Call DataBind() method

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    83/107

    Data BindingSimple Lists

    Demo: DataBinding2.aspx

    Data binding to simple lists

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    84/107

    Data BindingDatabase

    Data binding can be used to populate server

    controls with data from a database

    Each UI element corresponds to a row

    Bind to a DataReader (preferred)

    Bind to a DataView of a DataSet

    Specify value and text with DataValueField

    and DataTextField, respectively Each of these corresponds to a column

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    85/107

    Data BindingData Source Example

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    86/107

    Data BindingList Binding Examples

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    87/107

    Data BindingBinding to a Database

    Demo: DataBinding3.aspx

    Data binding to a database

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    88/107

    Data Binding

    Full-featured list output

    Default look is a grid

    Default is to show all columns, though you canspecify a subset of columns to display

    Columns can be formatted with templates

    Optional paging

    Updateable

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    89/107

    Data BindingBinding to All Columns

    Binding all columns in the datasource

    Declare an

    Set its DataSource

    Call DataBind()

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    90/107

    Data BindingBinding to Specific Columns

    By default, DataGrid will display all columns

    To control columns to display:

    Set AutoGenerateColumns=false

    Specify Columns property

    Add column definition

    BoundColumn

    TemplateColumn

    ButtonColumn, EditCommandColumn, HyperlinkColumn

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    91/107

    Data BindingBinding to Specific Columns

    Binding to specific columns in the datasource

    Declare an

    Declare its Columns collection

    Set its DataSource

    Call its DataBind() method

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    92/107

    Data BindingPaging

    When there is too much data to display in one

    screen, a DataGrid can provide automaticpaging

    Set AllowPaging=true

    Set PageSize=5

    Handle OnPageIndexChanged event

    Set page index

    Fetch data

    Re-bind data

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    93/107

    Data BindingDemo

    Demo: DataBinding4.aspx

    Binding to a database with DataGrid

    Demo: DataBinding5.aspx

    Paging through data with DataGrid

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    94/107

    Data BindingTemplates

    Templates provide a powerful way to customize the

    display of a server control

    Customize structure not just style

    Can use controls or other HTML within a template 3rd party controls can expose new templates

    With data binding, templates specify a set of markup

    (HTML or server controls) for each bound piece of data

    Not just specifying formatting and style for a column

    However, templates are not limited to data binding

    No fixed set of templates

    Controls may define their own and expose any number of them

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    95/107

    Data BindingTemplates

    Standard templates for list-bound controls

    HeaderTemplate: rendered once before all databound rows

    ItemTemplate: rendered once for each row in thedata source

    AlternatingItemTemplate: like ItemTemplate,but when present is used for every other row

    SeparatorTemplate: rendered between each row FooterTemplate: rendered once, after all data

    bound rows

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    96/107

    Data BindingTemplates

    SeparatorTemplate

    FooterTemplate

    HeaderTemplate

    AlternatingItem-

    Template

    ItemTemplate

    Templatesused inRepeater

    controls

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    97/107

    Data BindingData Binding in Templates

    Templates need to access the bound data

    Containeris an alias for the templatescontaining control

    DataItem is an alias for the current row of thedatasource

    DataBinder.Eval is a utility function provided

    to retrieve and format data within a template

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    98/107

    Data BindingControl

    Provides simple output of a list of items

    No inherent visual form

    Templates provide the visual form

    No paging

    Can provide templates for separators

    Not updateable

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    99/107

    Data BindingControl

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    100/107

    Data BindingControl

    Provides list output with editing

    Default look is a table

    Customized via templates

    Directional rendering (horizontal or vertical)

    Single and multiple selection

    Alternate item

    Updateable

    No paging

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    101/107

    Data BindingControl

    Data Binding

  • 7/29/2019 Intro to ASP.NET and Web Forms

    102/107

    Data BindingTemplates Demo

    Demo: DataBinding6.aspx, DataBinding7.aspx

    Using templates and data binding to a database withDataGrid, Repeater and DataList controls

    A d

  • 7/29/2019 Intro to ASP.NET and Web Forms

    103/107

    Agenda

    Background

    ASP.NET Overview

    Programming Model

    Programming Basics

    Server Controls

    Data Binding

    Conclusion

    C l i

  • 7/29/2019 Intro to ASP.NET and Web Forms

    104/107

    Conclusion

    We covered

    What ASP.NET and Web Forms are

    ASP.NET Programming Essentials

    Server Controls Data Binding

    Templates

    Conclusion

  • 7/29/2019 Intro to ASP.NET and Web Forms

    105/107

    Conclusion

    Next time, well cover

    Web Applications

    Configuration

    Tracing

    Session Management

    Error Handling

    Deployment

    Security

    Architecture

    Extensibility (User Controls and Custom Controls)

    Resources

  • 7/29/2019 Intro to ASP.NET and Web Forms

    106/107

    Resources

    General Sites http://msdn.microsoft.com/net/aspnet/default.asp

    http://www.asp.net/

    http://www.fmexpense.com/quickstart/aspplus/defa

    ult.htm http://www.asptoday.com/

    https://reader009.{domain}/reader009/html5/0412/5ace7 http://www.4guysfromrolla.com/

    http://www.aspfree.com/

    http://www.devx.com/dotnet/ http://www.ibuyspy.com/

    Resources

  • 7/29/2019 Intro to ASP.NET and Web Forms

    107/107

    Resources

    ASP.NET Overviewhttp://msdn.microsoft.com/msdnmag/issues/0900/ASPPlus/ASPPlus.asp

    Validationhttp://msdn.microsoft.com/library/techart/aspplusvalid.

    htm Databinding in 3 parts

    http://msdn.microsoft.com/msdnmag/issues/01/03/cutting/cutting0103.asphttp://msdn.microsoft.com/msdnmag/issues/01/04/cutting/cutting0104.asphttp://msdn.microsoft.com/msdnmag/issues/01/05/cutting/

    cutting0105.asp ASP.NET component model

    http://msdn.microsoft.com/msdnmag/issues/01/02/cutting/cutting0102.asp


Recommended