ASP .NET 2.0 - PowerPoint PPT Presentation

About This Presentation
Title:

ASP .NET 2.0

Description:

... which can be controlled by scripts ASP .NET contains a new set of programmable object-oriented input controls all ASP.NET objects on a web page can expose ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 24
Provided by: AdrianS153
Category:
Tags: asp | net | aspnet

less

Transcript and Presenter's Notes

Title: ASP .NET 2.0


1
ASP .NET 2.0
2
What is ASP .NET
  • is a server-side scripting language developed by
    Microsoft
  • is the next generation of ASP (Active Server
    Pages), completely new technology, not backward
    compatible to classic ASP
  • is part of Microsoft .Net framework
  • runs inside IIS (Internet Information Server)
  • version history ASP .NET, ASP .NET 2.0, ASP .NET
    3.0
  • an ASP .NET file has the extension .aspx as
    opposed to .asp as in classic ASP
  • the execution and role of .aspx files is the
    same as in other server-side languages like JSP,
    PHP, etc.

3
Microsoft .Net Framework
  • Microsoft .Net framework is an environment for
    building, deploying and running web-based and
    standalone enterprise applications
  • it contains the following components
  • programming languages C, Visual Basic .Net, J
  • server technologies and client technologies ASP
    .NET,
  • Windows Forms (Windows desktop solutions),
  • Compact Framework (PDA/Mobile solutions)
  • development environments Visual Studio .Net,
    Visual Web Developer

4
Differences between ASP .NET and ASP
  • ASP .NET has a new set of programmable controls,
    XML-based components, increased performance by
    running compiled code, event-driven programming
  • ASP .NET uses ADO .NET for accessing databases,
    supports full Visual Basic, not VBScript,
    supports C, C and JScript
  • ASP .NET has a large set of HTML controls which
    can be controlled by scripts
  • ASP .NET contains a new set of programmable
    object-oriented input controls
  • all ASP.NET objects on a web page can expose
    events that can be processed by ASP.NET code

5
Running ASP .NET
  • for running ASP .NET code only
  • .NET Framework
  • and IIS (Microsoft Internet Information Server)
  • is required
  • the principle of running ASP .NET code is the
    same as in running other server-side scripts like
    PHP and JSP

6
First .aspx example
  • lthtmlgtltbodygtltpgtltResponse.Write(Hello .Net
    World!)gtlt/pgtlt/bodygt
  • lt/htmlgt
  • in classic ASP (and also available in ASP .NET),
    ASP code is inserted in html code inside the tags
    ltgt

7
ASP .NET Server Controls
  • in ASP .NET, ASP code is not placed entirely
    inside the html code, but instead is written in
    separate executable files
  • server controls are tags understood and run by
    the server
  • there are three kinds of server controls
  • HTML Server Controls - Traditional HTML tags
  • Web Server Controls - New ASP.NET tags
  • Validation Server Controls - For input validation

8
HTML Server Controls
  • html elements in ASP.NET files are, by default,
    treated as text. To make these elements
    programmable a runat"server" attribute to the
    html element is required. This attribute
    indicates that the element should be treated as a
    server control. The id attribute is added to
    identify the server control.
  • all html server controls must be within a ltformgt
    tag with the runat"server" attribute. The
    runat"server" attribute indicates that the form
    should be processed on the server. It also
    indicates that the enclosed controls can be
    accessed by server scripts.

9
HTML Server Control example
  • ltscript runat"server"gt
  • Sub Page_Load link1.HRef"http//www.g
    oogle.com"End Sub
  • lt/scriptgt
  • lthtmlgt
  • ltbodygt
  • ltform runat"server"gtlta id"link1"
    runat"server"gtVisit Google!lt/agtlt/formgt
  • lt/bodygt
  • lt/htmlgt

10
Web Server Controls
  • are special tags understood by the server, but
    they do not map to any existing HTML element
  • they require a runatserver attribute to work
  • ltaspcontrol_name id"some_id" runat"server" /gt
  • ex.
  • ltscript runat"server"gtSub submit(Source As
    Object, e As EventArgs)button1.Text"You clicked
    me!"End Sub
  • lt/scriptgt
  • lthtmlgt
  • ltbodygtltform runat"server"gt ltaspButton
    id"button1" Text"Click me!" runat"server"
    OnClick"submit"/gtlt/formgt
  • lt/bodygt
  • lt/htmlgt

11
Validation Server Controls
  • are used to validate user input if the user
    input does not pass validation, it will display
    an error message
  • each validation control performs a specific type
    of validation
  • ltaspcontrol_name id"some_id" runat"server" /gt
  • ex.
  • lthtmlgt
  • ltbodygt
  • ltform runat"server"gt ltpgtEnter a number from 1
    to 100 ltaspTextBox id"tbox1" runat"server"
    /gtltbr /gtltbr /gt ltaspButton Text"Submit"
    runat"server" /gt lt/pgt ltpgt ltaspRangeValidator
    ControlToValidate"tbox1 MinimumValue"1" Maximu
    mValue"100 Type"Integer Text"The value must
    be from 1 to 100!" runat"server"
    /gt lt/pgtlt/formgt
  • lt/bodygtlt/htmlgt

12
Event handlers
  • ex. Page_Load event handler
  • ltscript runat"server"gtSub Page_Load lbl1.Text"
    The date and time is " now()End Sub
  • lt/scriptgt
  • lthtmlgtltbodygtltform runat"server"gt lth3gtltasplabe
    l id"lbl1" runat"server" /gtlt/h3gtlt/formgt
  • lt/bodygt
  • lt/htmlgt

13
ASP .NET Web Forms
  • all server controls must be defined within a
    ltformgt tag and this should have the attribute
    runatserver
  • there can be only one ltform runatservergt
    control on a web page
  • the form is always submitted to the page itself
    regardless of the action attribute
  • the method is set to POST by default
  • if not specified, name and id attribute are
    automatically assigned by ASP .NET

14
ASP .NET ViewState
  • ASP .NET maintains a ViewState (containing all
    the values of the fields in the form) as a hidden
    field in the page like this
  • ltinput type"hidden" name"__VIEWSTATE"value"dD
    wtNTI0ODU5MDE1OzsZBCF2ryjMpeVgUrY2eTj79HNl4Q"
    /gt
  • this is useful when the frame is submitted to the
    server with an error and you have to go back and
    correct the input data on the form
  • maintaining the ViewState is the default setting
    for Web forms this can be changed with the
    directive
  • lt_at_ Page EnableViewState"false" gt

15
aspTextBox
  • ltscript runat"server"gtSub change(sender As
    Object, e As EventArgs) lbl1.Text"You changed
    text to " txt1.TextEnd Sub
  • lt/scriptgt
  • lthtmlgtltbodygtltform runat"server"gt Enter your
    name ltaspTextBox id"txt1" runat"server
    text"Hello World!" ontextchanged"change"
    autopostback"true"/gt ltpgtltaspLabel id"lbl1"
    runat"server" /gtlt/pgtlt/formgtlt/bodygtlt/htmlgt

16
aspButton
  • ltscript runat"server"gtSub submit(sender As
    Object, e As EventArgs) lbl1.Text"Your name is
    " txt1.TextEnd Sub
  • lt/scriptgt
  • lthtmlgtltbodygtltform runat"server"gt Enter your
    name ltaspTextBox id"txt1" runat"server"
    /gt ltaspButton OnClick"submit" Text"Submit"
    runat"server" /gt ltpgtltaspLabel id"lbl1"
    runat"server" /gtlt/pgtlt/formgt
  • lt/bodygt
  • lt/htmlgt

17
Data binding controls
  • some controls can be bind to a datasource like a
    database, an xml file or a script
  • the data binding list controls are
  • aspRadioButtonList
  • aspCheckBoxList
  • aspDropDownList
  • aspListBox
  • these controls can contain ltaspListItemgt or they
    can use the items from a data source

18
Binding an ArrayList to a aspRadioButtonList
  • ltscript runat"server"gtSub Page_Loadif Not
    Page.IsPostBack then dim mycountriesNew
    ArrayList mycountries.Add("Norway") mycountries.
    Add("Sweden") mycountries.Add("France") mycountr
    ies.Add("Italy") mycountries.TrimToSize() mycoun
    tries.Sort() rb.DataSourcemycountries rb.DataBi
    nd()end ifend sub
  • lt/scriptgt
  • lthtmlgtltbodygtltform runat"server"gt ltaspRadioBut
    tonList id"rb" runat"server" /gtlt/formgt
  • lt/bodygtlt/htmlgt

19
Binding an xml file to a aspRadioButtonList
  • lt_at_ Import Namespace"System.Data" gt
  • ltscript runat"server"gtsub Page_Load if Not
    Page.IsPostBack then dim mycountriesNew
    DataSet mycountries.ReadXml(MapPath("countries.xm
    l")) rb.DataSourcemycountries rb.DataValueField
    "value" rb.DataTextField"text" rb.DataBind()
    end ifend sub
  • sub displayMessage(s as Object,e As
    EventArgs) lbl1.text"Your favorite country is
    " rb.SelectedItem.Textend sub
  • lt/scriptgt
  • lthtmlgt
  • ltbodygtltform runat"server"gt ltaspRadioButtonList
    id"rb" runat"server" AutoPostBack"True"
    onSelectedIndexChanged"displayMessage"
    /gt ltpgtltasplabel id"lbl1" runat"server"
    /gtlt/pgtlt/formgt
  • lt/bodygtlt/htmlgt

20
Creating a Database Connection using ADO .NET
  • lt_at_ Import Namespace"System.Data.OleDb" gt
  • ltscript runat"server"gtsub Page_Load dim
    dbconn dbconnNew OleDbConnection("ProviderMicro
    soft.Jet.OLEDB.4.0 data source"
    server.mappath("northwind.mdb")) dbconn.Open()en
    d sub
  • lt/scriptgt

21
Creating a Database Command
  • lt_at_ Import Namespace"System.Data.OleDb" gt
  • ltscript runat"server"gtsub Page_Load dim
    dbconn,sql,dbcomm dbconnNew OleDbConnection("Pro
    viderMicrosoft.Jet.OLEDB.4.0 data source"
    server.mappath("northwind.mdb")) dbconn.Open() s
    ql"SELECT FROM customers" dbcommNew
    OleDbCommand(sql,dbconn)end sub
  • lt/scriptgt

22
Creating a DataReader
  • lt_at_ Import Namespace"System.Data.OleDb" gt
  • ltscript runat"server"gtsub Page_Load dim
    dbconn,sql,dbcomm,dbread dbconnNew
    OleDbConnection("ProviderMicrosoft.Jet.OLEDB.4.0
    data source" server.mappath("northwind.mdb")
    ) dbconn.Open() sql"SELECT FROM
    customers" dbcommNew OleDbCommand(sql,dbconn) d
    breaddbcomm.ExecuteReader()end sub
  • lt/scriptgt

23
Binding a Datasource to a control
  • lt_at_ Import Namespace"System.Data.OleDb"
    gtltscript runat"server"gtsub Page_Load dim
    dbconn,sql,dbcomm,dbread dbconnNew
    OleDbConnection("ProviderMicrosoft.Jet.OLEDB.4.0
    data source" server.mappath("northwind.mdb"))
    dbconn.Open() sql"SELECT FROM
    customers" dbcommNew OleDbCommand(sql,dbconn) d
    breaddbcomm.ExecuteReader() customers.DataSource
    dbread customers.DataBind() dbread.Close() dbc
    onn.Close()end sublt/scriptgtlthtmlgtltbodygt ltfor
    m runat"server"gt ltaspRepeater id"customers"
    runat"server"gt ltHeaderTemplategt lttable
    border"1" width"100"gt lttrgt ltthgtCompanynam
    elt/thgt ltthgtContactnamelt/thgt ltthgtAddresslt/thgt
    ltthgtCitylt/thgt lt/trgt lt/HeaderTemplategt lt
    ItemTemplategt lttrgt lttdgtltContainer.DataItem
    ("companyname")gtlt/tdgt lttdgtltContainer.DataIte
    m("contactname")gtlt/tdgt lttdgtltContainer.DataIt
    em("address")gtlt/tdgt lttdgtltContainer.DataItem(
    "city")gtlt/tdgt lt/trgt lt/ItemTemplategt ltFoote
    rTemplategt lt/tablegt lt/FooterTemplategt lt/asp
    Repeatergt lt/formgtlt/bodygtlt/htmlgt
Write a Comment
User Comments (0)
About PowerShow.com