Docstoc

Asp.net

Document Sample
Asp.net Powered By Docstoc
					Advanced List Controls

•   There are some advanced list controls like Repeater, DataList, and DataGrid

•   They take collections of data and loop through them automatically.

•   They act as containers for other controls that actually display the data such as
    labels.

•   These controls are very powerful and save developers a lot of manual work.
Repeater Control

•   Provides simple output of a list of items

•   Templates provide the visual form

•   It iterates over the bound data, rendering its ItemTemplate once for each item
    in the DataSource collection

•   Useful to have complete control over how data from a data source is rendered

•   No paging

•   Can provide templates for separators

•   Does not provide update data
Repeater Control

•   Standard templates for Repeater controls

     – HeaderTemplate: rendered once before all data bound rows
     – ItemTemplate: rendered once for each row in the data 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
Repeater Control

•   To bind embedded controls to the data source of the container control
    DataBinder.Eval() method is used

•   DataBinder.Eval() method is provided by .net to evaluate expression

•   Syntax
     –   <%# DataBinder.Eval(Container.DataItem,“DataFieldname")   %>

     –   Container represents the container for data items

     –   DataField represents the name of data item field



          DEMO
DataList Control

•   Provides list output with editing

•   Default look is a table

•   Customized via templates

•   Directional rendering (horizontal or vertical)

•   Alternate item

•   Updateable

•   No paging
DataGrid

•   Full-featured list output

•   Default look is a grid

•   Default is to show all columns, though you can specify a subset of columns to
    display

•   Columns can be formatted with templates

•   Optional paging

•   Updatable



DEMO
ASP.NET built in objects

•   Application Object
     –   Top Level object in application’s Object hierarchy

•   Page Object
     –   Controls User interface
Application Object

•   Used to configure the application

•   Save State information

•   To respond to application wide events

•   Has properties and methods that provide access to other objects in the
    application
Application Object – Properties and Methods

•   Application
     –   To save data item in application state

•   Context
     –   Get Handler, Trace, Cache, Error and other object in current Context

•   Modules
     –   Access HTTP Modules

•   Request
     –   Read a Request

     –   Get Browser, ClientCertificate, Cookies and File objects from current request
Application Object – Properties and Methods

•   Server
     –   Process request and response

     –   Has methods to help URL encoding and decoding

•   Session
     –   To save data item in Session state

•   User
     –   Get authentication information about user making current request
Page Object – Properties and Methods
Page Object – Properties and Methods

•   Controls User interface

•   Application
     – To save data item in Application state
•   Cache
     – Controls how responses are cached on Server
•   Controls
     – Get at controls on the page
•   Request
     – Read a Request
     – Get Browser, ClientCertificate, Cookies and File objects from current request
Page Object – Properties and Methods
•   Response
     – Write text/data to a response
     – Get Cache, Cookies and output object for current response
•   Server
     – Process request and response
     – Has methods to help URL encoding and decoding
•   Session
     – To save data item in Session state
•   Trace
     – Turn trace On or Off and write to trace log.
Request Object
•   Contains information sent by the client browser

•   Browser

     –   Determines the capabilities of the browser

     –   Browser properties like Version Number, Whether Cookies enabled etc.

•   ClientCertificate

     –   Authenticate the client

•   Cookies

     –   Get information about client through cookies

•   Files

     –   Gets files that are unloaded by the client

•   InputStream

     –   Read and Write to Raw data sent in the request
Response Object

•   Response sent by server to the client

•   Cache
     –   Determines how server caches responses before being sent to client

•   Cookies
     –   Set the content of the cookies sent to the client

•   Output
     –   Get or set the raw data returned to the client as the response
Session and Application objects

•   Session

     – Provides information to the current user session.
     – Session management
•   Application

     – Provides access to application-wide methods and events for all sessions.
     – Provides access to an application-wide cache
State Management is ASP.NET

•   Web forms are created and destroyed each time a client browser makes a
    request.

•   Variables declared within a Web form do not retain their value after a page is
    destroyed.

•   In ASP.NET application ,the state management feature is implemented using
    client side options and the server side options .
State Management is ASP.NET

Client- side options   Server- side options


View State property    Application state


Cookies                Session State



Query Strings          Database support
View State

•   Use the ViewState property to save data in a hidden field on a page

•   Each control on a Web Forms page, including the page itself, has a ViewState
    property that it inherits from the base Control class

•   ViewState is generally used for persisting form data on a page across round
    trips.

•   Do not use ViewState to store information such as passwords, connection
    strings, and file paths. For information about sharing data across pages or
    more persistent storage
Cookies

•   Cookies :
     –   to store small amounts of information on a client.

     –   provide a useful means in Web applications to store user-specific information



•   Clients might refuse cookies, so your code has to anticipate that possibility.



          DEMO
Query Strings

•   Query Strings

     – Query strings are a way to pack information into a link
     – then retrieve that information on the page that was linked to.
     – The URL with a query string might look like this

     http://localhost/DemoDay2BaseLine/DataGridDemo1.aspx?id=value



     DEMO
Session Objects

•   Session state :

     Can store items that we want keep local to the current session (single user).

     – We can store values that need to be persisted for the duration of a user.
     – Every user session will be assigned unique session Id.
Configuration

•   Configuration information for ASP.NET resources is contained in a collection of
    configuration files, each named Web.config.

•   Each configuration file contains a nested hierarchy of XML tags and sub tags
    with attributes that specify the configuration settings.

•   Because the tags must be well-formed XML, the tags, sub tags, and attributes
    are case-sensitive.

     – The first character of a tag name is lowercase and the first letter of any
        subsequent concatenated words is uppercase.

     – Attribute values   first character is uppercase and the first letter of any
        subsequent concatenated words is uppercase.
Configuration

•   Configuration files can be stored in application folders
     –   Configuration system automatically detects changes

•   Hierarchical configuration architecture
     –   Applies to the actual directory and all subdirectories




                       Root                      web.config
                        Dir
                                    Sub
                                    Dir1
                                                 Sub
                                                 Dir2
Configuration
<configuration>
    <configsections>
      <add names=“httpmodules“
       type=“System.Web.Config.HttpModulesConfigHandler“/>
      <add names=“sessionState“
        type=“...“/>
    </configsections>


    <httpModules>
      <!--- http module subelements go here -->
    </httpModules>


 <sessionState>
      <!--- sessionstate subelements go here -->
    </sessionState>
   </configuration>
Configuration
–<configuration>
– <appSettings>
–    <add key=“CxnString”

–      value=“localhost;uid=sa;pwd=;Database=foo”/>

– </appSettings>
–</configuration>

 Retrieve settings at run-time


 string cxnStr = ConfigurationSettings
                   .AppSettings["CxnString"];
Machine.config

•   ASP.NET uses machine-wide configuration from machine.config
     –   ASP.NET configuration inside <system.web> element

     –   <httpRuntime> determines how requests are processed
Session Management

•   Limitations with ASP session Management
     –   Process dependent

     – Server farm limitations
     – Cookie dependent
Session Management

•   Process independent
     –   ASP.NET session state is able to run in a separate process from the ASP.NET host
         process.



•   Support for server farm configurations
     –   By moving to an out-of-process model, ASP.NET also solves the server farm
         problem
Session State

•   Session state can be stored in three ways
     –   InProc

     –   StateServer

     –   SQLServer



     DEMO
Session State
•   InProc
         –   Stores values in the memory of the ASP. net worker process.

         –   Provides faster access to these values

         –   When the asp.net worker process is recycled data is lost

<sessionState

             mode="InProc"

             stateConnectionString="tcpip=127.0.0.1:42424"

             sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"

             cookieless="false"

             timeout="20"

    />
Session State

•   State Server:
     –   This mode uses a stand alone window services to store session variables

     –   This service is independent of IIS as it can run on separate server

     –   This can be mainly used for load balancing ,b’coz multiple server can share their
         session information

     <sessionState

              mode="StateServer"

              stateConnectionString="tcpip=127.0.0.1:42424"

              cookieless="false"

              timeout="20"

         />
Session State
•   SQL Server Mode
     – The SQL Server mode option is similar to that of the Windows Service,
       except that the information persists to SQL Server rather than being stored
       in memory.


<sessionState
       mode=“SQLServer"
       sqlConnectionString="data source=127.0.0.1;user
        id=sa;password=lathasiddu@1995"
        cookieless="false"
        timeout="20"
/>
Application Object

•   Application Object
     –   object provides a mechanism for storing data that is accessible to all code running
         within the Web application.

     –   We store application wide variables in a special file called as Global.asax

•   Advantages
     –   Can do a one-time loading and calculation of information.

•   Disadvantages
     –   The memory occupied by variables stored in application state is not released until
         the value is either removed or replaced.

     –   Keeping seldom-used 10 MB record sets in application state permanently.
Global.asax

•   Located at application root

•   Can contain user-created code to handle application and session events (just
    like ASP)
     –   Application_OnStart, Application_OnEnd

     –   Session_OnStart, Session_OnEnd

     Demo


    void Application_OnStart() {
      Application["startTime"]=DateTime.Now.ToString();
    }
    void Session_OnStart() {
      Session["startTime"]=DateTime.Now.ToString();
    }
Navigating between Forms
       Navigation method                                 Use to

Hyperlink control           Navigate to another page.

                            Navigate to another page from code. This is equivalent to
Response.Redirect method
                            clicking a hyperlink.

                            End the current Web form and begin executing a new Web
Server.Transfer method      form. This method works only when navigating to a Web
                            Forms page (.aspx).

                            Begin executing a new Web form while still displaying the
                            current Web form. The contents of both forms are
Server.Execute method       combined.
                            This method works only when navigating to a Web Forms
                            page (.aspx).


Window.Open script method
                            Display a page in a new browser window on the client.
Error Handling

•   .NET Common Language Runtime provides structured Exception handling
    using try catch block.

•   ASP.NET provides declarative application custom error handling
     –    Automatically redirect users to error page when unhandled exceptions occur

     –    Prevents ugly error messages from being sent to users

    <customErrors
          mode="RemoteOnly"

     />
Error Handling

Mode attribute is either set into On or RemoteOnly .

•   On

    –    Error messages will be displayed in client and well as server where
         application is running

•   RemoteOnly

    –    Error messages will be displayed only at client location
Error Handling

•   Custom Error Pages

•   Can specify error pages for specific HTTP status codes in web.config


      <configuration>
        <customerrors mode=“remoteonly”
                      defaultredirect=“error.htm”>
          <error statuscode=“404”
                 redirect=“adminmessage.htm”/>
          <error statuscode=“403”
                 redirect=“noaccessallowed.htm”/>
        </customerrors>
      </configuration>

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:9
posted:6/22/2012
language:English
pages:39