ASP.NET - PowerPoint PPT Presentation

1 / 80
About This Presentation
Title:

ASP.NET

Description:

... from its storage directory on the server and to make ... In this fashion the entire page becomes a form within which server controls can be placed. ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 81
Provided by: Ege2
Category:
Tags: asp | net

less

Transcript and Presenter's Notes

Title: ASP.NET


1
ASP.NET
2
ASP.NET Environment
  • ASP.NET is Microsoft's programming framework that
    enables the development of Web applications and
    services.
  • It is an easy and scalable way to build, deploy,
    and run Web applications that can target any
    browser or device.
  • ASP.NET pages work in all browsers -- Navigator,
    Opera, AOL, Internet Explorer, and others.

3
ASP.NET Environment
  • Many of the functionalites take place in the
    background, their details hidden from view of,
    and usually hidden from concern by, the
    programmer.
  • ASP.NET has a rich set of objects to work with in
    an object-oriented and compiled programming
    environment.

4
ASP.NET Environment
  • The programming environment supports more than 25
    .NET languages, including built-in support for
    VB.NET, C, and JScript.NET.
  • .NET Framework offers over 4500 software classes
    that encapsulate rich functionality like XML,
    data access, file upload, regular expressions,
    image generation, performance monitoring and
    logging, transactions, SMTP mail, and much more.

5
ASP.NET Environment
  • These classes provide an extensive set of
    objects, properties, and methods accessible
    through Visual Basic for enhancing your Web
    applications.
  • Though code is compiled, ASP.NET automatically
    detects any code changes, dynamically compiles
    the files if needed, and stores the compiled
    results for reuse in subsequent requests.

6
ASP.NET Environment
  • Dynamic compilation ensures that applications are
    up to date, and compiled execution makes them
    fast. There is a short delay when a page is first
    accessed and compiled. Subsequent accesses,
    though, retrieve the compiled, cached version of
    the page for immediate execution.

7
ASP.NET Environment
  • The ASP.NET framework is backward compatible. It
    runs classic ASP pages just as before, permitting
    integration of new capabilities as they are
    learned. ASP.NET pages have a new file extension,
    .aspx, to differentiate them from standard ASP
    pages. When this new extension is detected, the
    ASP.NET processor handles the page request.

8
ASP.NET Environment
  • User interaction under ASP.NET, is through a Web
    browser. New Web Form controls in ASP.NET replace
    many of the standard HTML tags.

9
ASP.NET Page Structure
  • ASP.NET pages are transmitted to the Web server
    in order to process data and generate
    information. Normally, this request is triggered
    by some user action on the local PC. For
    instance, the user enters data into a form and
    then clicks a button. The button click causes the
    form to be transmitted to the server where
    processing routines massage the data and produce
    output. Output usually is embedded on the Web
    page prior to its return to the user.

10
ASP.NET Page Structure
  • Web page processing takes place on the server.
    Data are collected on the local PC, shipped to
    the server for processing, with results returned
    to the local PC as a reformulated or reformatted
    page displaying processing results.

11
ASP.NET Page Structure

12
ASP.NET Page Structure
  • The SCRIPT portion of the page, normally coded at
    the top of the page, contains server code to
    process data. That data can be supplied by the
    user through a form, extracted from files or
    databases, and/or generated internally by the
    script. The script contains processing commands,
    or statements, written in Visual Basic, which are
    encapsulated within named subroutines, or
    subprograms, that are called upon to perform
    their processing.

13
ASP.NET Page Structure
  • The HTML portion of the page serves two
    functions. It captures input data for
    transmission to the server, and it displays
    output results from server processing.
  • These two functions take place through Web Forms
    containing various server controls, special form
    elements that permit data input, output display,
    and requests for script processing.

14
ASP.NET Exa.aspx
  • An asp file can contain only HTML code. You can
    create a web page using Front Page and save it to
    c/inetpub/wwwroot directory and giving aspx
    as file extension.
  • You call the page from localhost using
  • http//localhost/exa.aspx
  • Your page code can be
  • lthtmlgtltbodygt HELLO lt/bodygtlt/htmlgt

15
ASP.NET Ex.aspx
  • You can call an aspx file from another file.
  • Your page code can be
  • ltscript runat"server"gt
  • Sub Display_Output(Src As Object, Args As
    EventArgs)
  • response.redirect ("exa.aspx")
  • End Sub
  • lt/scriptgtlthtmlgtltbodygt
  • ltform runat"server" gt
  • ltaspButton id"Button1" onclick"Display_Output"
    runat"server" Text"Go to Hello Page"gt
  • lt/aspButtongt
  • lt/formgtlt/bodygtlt/htmlgt

16
ASP.NET Page Structure
  • Together, Web Forms and server scripts
    encapsulate on a single page all the
    functionality needed to turn Web pages into
    full-featured information processors.
  • It is common for ASP.NET pages to supply three
    types of HTML form controls to effect information
    processing.

17
ASP.NET Page Structure
  • A "data input control" is needed to supply data
    for processing
  • A "script activation control" is needed to call
    upon scripts to process the data
  • An "output control" is required to identify an
    area on the page where script output can appear.

18
ASP.NET Ex1.aspx
  • A textbox control accepts input data from the
    user, a button calls a script to process the
    data, and an output control provides the target
    area for script output.

19
ASP.NET Ex1.aspx
  • ltSCRIPT runat"server"gtSub Display_Output(Src
    As Object, Args As EventArgs)  MyOutput.Text
    "Welcome, " MyInput.Text ", to the world of
    ASP.NET."End Sublt/SCRIPTgt

20
ASP.NET Ex1.aspx
  • In this case the subprogram simply takes the
    value entered into the TextBox and writes it,
    along with other enclosing strings of text, to
    the Label control.
  • Note that the script, the controls, and their
    enclosing ltformgt tag contain the attribute
    runat"server". This specification indicates that
    all of these page elements participate in script
    processing on the server.

21
ASP.NET Ex1.aspx
  • lthtmlgtltbodygtltform runat"server"gtEnter your
    first name ltbrgtltaspTextBox id"MyInput"
    runat"server"/gtltaspButton Text"Click Me"
    OnClick"Display_Output" runat"server"/gtltaspLab
    el id"MyOutput" runat"server"/gtlt/formgtlt/bodygtlt/
    htmlgt

22
ASP.NET Ex1.aspx
  • In this example an ltaspTextBoxgt control supplies
    the input box. An ltaspButtongt control calls a
    script subroutine named Display_Output when
    clicked. An ltaspLabelgt control serves as an area
    on the page to which the script writes its
    output. Both the TextBox and Label controls are
    given id values so that the script can refer to
    them when performing its processing.

23
ASP.NET Ex1.aspx
  • The script appearing at the top of the page
    contains the subroutine named Display_Output. The
    associated list of arguments contained within
    parentheses (Src As Object, Args As EventArgs)
    are references to the particular server control
    which calls the subroutine -- the button in this
    case.

24
ASP.NET Ex1.aspx
  • The Src (source) parameter is a reference to the
    source of the subroutine call -- the button
    itself the Args (arguments) parameter is a
    pointer to any data values associated with the
    button -- none in this example. These are
    standard arguments that must be supplied when a
    button calls a subprogram.

25
ASP.NET Ex1.aspx
  • The button click triggers a form submission which
    transmits the controls and their values to the
    Web server. At the same time, a URL request is
    made of the server to retrieve a new copy of this
    same Web page from its storage directory on the
    server and to make available to this page the
    transmitted values. The button click also
    indicates that when the page is retrieved the
    subprogram named Display_Output is to be run.

26
ASP.NET Ex1.aspx
  • This subprogram copies the transmitted data from
    the Textbox into the Label area, embedding it
    within the other text and HTML on the page.
    Following script processing this reformulated
    page is returned from the server to your PC, with
    your name appearing on the page.

27
ASP.NET Ex2.aspx
  • Ex1.aspx can be changed to hide the textbox and
    button after user clicks the button.
  • We will use visible property of the object and
    set it to false in order to hide it.
  • id will be used as object name.
  • If id is MyInput then property can be set as
  • MyInput.visiblefalse

28
ASP.NET Ex2.aspx
  • ltscript runat"server"gt
  • Sub Display_Output(Src As Object, Args As
    EventArgs)
  • MyOutput.Text "Welcome, " MyInput.Text
  • MyInput.visiblefalse
  • Button1.visiblefalse
  • End Sub
  • lt/scriptgt
  • lthtmlgtltbodygt
  • ltform runat"server"gt
  • ltaspTextBox id"MyInput"
    runat"server"gtlt/aspTextBoxgt
  • ltaspButton id"Button1"
    onclick"Display_Output" runat"server"
    Text"Click Me"/gt
  • ltaspLabel id"MyOutput" runat"server/gt
  • lt/formgt
  • lt/bodygtlt/htmlgt

29
ASP.NET and HTML
  • Most of HTML standards have been replaced by the
    newer server controls -- ltaspTextbox/gt,
    ltaspButton/gt, and ltaspLabel/gt. These are
    special tags that are characterized by their
    accessibility by server scripts. All of these
    controls have the asp prefix and contain the
    runat"server" attribute.
  • Server controls encapsulate within their
    functionality much of the HTML that would
    otherwise be coded on the page. As a result, Web
    pages tend to become large blocks of script and
    tiny blocks of HTML. Yet, the HTML that remains
    -- primarily the server controls -- themselves
    generate much of the HTML that appears on the
    output page.

30
ASP.NET Page Components
  • ltSCRIPT language"vb" runat"server"gt  Sub
    Page_Load    ...Visual Basic statements  End
    Sub  Sub MySubroutine (Src As Object, Evt As
    EventArgs)    ...Visual Basic statements  End
    Sublt/SCRIPTgt

31
ASP.NET Page Components
  • lthtmlgtltbodygtltform runat"server"gt  ltaspinput
    _control runat"server" /gt  ltaspscript_control
    runat"server" /gt  ltaspoutput_control
    runat"server" /gt  lt inline code gt  lt
    inline expression gtlt/formgtlt/bodygtlt/htmlgt

32
ASP.NET Script Blocks
  • Visual Basic code is contained within script
    blocks, normally appearing at the top of the Web
    page immediately following any directives. These
    script blocks contain code that is executed by
    the server when the page is retrieved and prior
    to sending it to the browser.
  • ltSCRIPT language"vb" runat"server"gt...lt/SCRI
    PTgt

33
ASP.NET Script Blocks
  • The word "SCRIPT" does not need to be coded in
    upper-case letters it is done in these tutorials
    for visual emphasis. The entry language"vb" or
    language"visual basic" is optionally coded in
    the opening ltSCRIPTgt tag and is provided as
    documentation.
  • Visual Basic is the default language if none is
    specified. It is necessary, though, to include
    the entry runat"server" to differentiate server
    scripts from any browser-side JavaScripts that
    might appear on the page. There can be one or
    more script blocks on a page, although one is
    usually sufficient.

34
ASP.NET Subroutines
  • Script blocks contain Visual Basic code packaged
    as named subprograms, or subroutines, which may
    contain an argument list enclosed in parentheses.
  • Sub Subroutine_Name (auguments)  ...End Sub
  • These subroutines contain the statements to carry
    out page processing, usually in response to user
    requests made through HTML form controls
    appearing on the page. As many subroutines are
    coded as are needed to perform the processing
    activities of the page.

35
ASP.NET Subroutines
  • A subroutine may be supplied with a required list
    of arguments -- a signature -- contained within
    parentheses and following the subroutine name.
  • Arguments are items of control information needed
    by the subroutine to perform its tasks. If a
    subroutine is run in response to a server contol
    on the page, two arguments are required in order
    to identify
  • 1. the object that triggered the subroutine
    call2. the event arguments accompanying that
    object

36
ASP.NET Subroutines
  • In the following example script, subprogram
    MySubroutine is activated when the user clicks a
    button. The signature for a subroutine called by
    a button click includes two arguments.
  • ltSCRIPT runatservergtSub MySubroutine (Src As
    Object, Args As EventArgs)  ...Visual Basic
    statementsEnd Sublt/SCRIPTgt

37
ASP.NET Subroutines
  • The terms Src and Args are programmer-supplied
    names. The first is declared As Object the
    latter is declared As EventArgs. The name Src is
    a reference to the button object that was clicked
    to call the subroutine. This name can be used by
    the script to determine properties of the clicked
    button. For example, the reference Src.id points
    to the id value assigned to the button control.
    This information can be used to determine which
    of possibly several buttons was clicked to call
    the subprogram.

38
ASP.NET Subroutines
  • The name Args is a reference to any accompanying
    data values associated with the button click.
    Certain types of buttons supply additional
    properties that can be access through this
    argument.
  • The list of arguments that are supplied to a
    subroutine depends on its purpose and on the type
    of control used to call it. Some subroutines may
    not require an argument list.

39
ASP.NET Variable Declarations
  • Script variables are data storage areas required
    by processing routines to temporarily hold data
    values generated by scripts. Any variables
    appearing in a Visual Basic subroutine must be
    declared prior to their use.
  • Dim VariableName As type The keyword Dim
    declares and identifies a programmer-supplied
    variable name. The As type clause is optional as
    a declaration of the data type, but there are
    efficiencies associated with doing so. Various
    data types are introduced in these tutorials as
    needed. Assignment of an initial value for the
    variable can also appear in the declaration,
  • Dim MyCounter As Integer 0 or, declaration and
    value assignment can appear as separate
    statements
  • Dim MyCounter As Integer MyCounter 0

40
ASP.NET Variable Declarations
  • Variables declared outside of subroutines are
    global and can be referenced from all
    subroutines. Variables that are declared inside a
    subroutine are local to that subroutine and
    cannot be accessed from other subroutines.
  • In the following example, variable G_Counter is
    declared globally since it appears outside of any
    subroutines in the script block it is,
    therefore, accessible by all subroutines.
    Variable L_Counter is declared inside subroutine
    IncrementLocalCounter therefore, it can be
    referenced only by that suboutine. It is normally
    preferred to declare variables locally to the
    subroutines that use them unless they necessarily
    need to be accessed globally.

41
ASP.NET Variable Declarations
  • ltSCRIPT runat"server"gtDim G_Counter As Integer
     'Define global variable named G_CounterSub
    InitializeCounterG_Counter 0
                 'Initialize the global variable
    G_CounterEnd SubSub IncrementCounterG_Counter
    1             'Add 1 to the global variable
    G_CounterEnd SubSub IncrementLocalCounterDim
    L_Counter As IntegerL_Counter 1
                'Add 1 to the local variable
    L_CounterEnd Sublt/SCRIPTgt

42
ASP.NET Variable Declarations
  • Although explicitly declaring variables in a Dim
    statement is the default setting for pages, the
    directive lt_at_ Explicit"False" gt can be included
    at the top of a page to permit use of variables
    without explicit declaration.
  • These variables are treated as Variant data types
    until converted for use according to the
    operations performed on them. It is slightly more
    efficient to explicitly declare variables so that
    computer memory can be pre-allocated for the data
    types.

43
ASP.NET Script Comments
  • Comments are non-executable lines of text added
    to a script in order to document or to explain to
    the reader the use of the code. Single-line
    comments are indicated with an apostrophe (')
    preceding the comment. Multiline comments are
    made by including commentary lines between lt--
    and --gt symbols. It is often useful to enclose
    executable code inside comments to render it null
    in the course of debugging scripts.
  • Visual Basic statement     ' This is a
    single-line commentlt--This is acomment
    spanningseveral lines.--gt

44
ASP.NET Statement Continuation
  • The underscore character ( _ ) is used as a
    line-continuation symbol when coding statements
    across two or more lines. The reason for doing
    this is to make the code more readable in the
    text editor by avoiding excessively long lines of
    code.
  • TextString "This is a very long string of text
    " _            "that is composed of several
    text strings" _            "that are
    concatenated to keep from coding " _           
    "a very long single statement."
  • The first three lines include a continuation
    character at the end of the line to indicate
    continuation of this single statement to the next
    line. Note that a single blank space must appear
    before the line-continuation symbol.

45
ASP.NET Linking Scripts
  • Scripts are normally activated in response to
    user actions surrounding a server control. Most
    commonly, buttons or links are the controls used
    to activate scripts. These controls include event
    handlers to respond to mouse clicks and to call
    named subroutines to perform processing when
    clicked. For example, the following button
    includes an OnClick event handler to call
    subprogram Process_Data when the button is
    clicked.
  • ltaspButton Text"Click Me" OnClick"Process_Data"
    /gt

46
ASP.NET Linking Scripts
  • Other controls provide input data or processing
    selections or identify locations on the HTML page
    where script output is to appear. Scripts read
    from and write to these controls by referencing
    or assigning values to the various properties of
    the controls.

47
ASP.NET Inline Script
  • In addition to appearing within separate script
    blocks, Visual Basic code can appear as inline
    scripts within the body of the HTML document.
    These "embedded" scripts are surrounded by lt ...
    gt characters to set them off from the
    surrounding HTML.

48
ASP.NET Inline Script
  • ltSCRIPT runat"server"gt  Dim Message As
    Stringlt/SCRIPTgtlthtmlgtltbodygt
  • lt Message"My message." gt ' an inline
    scriptltpgtHere is HTML code that displays _
  • lt Message gtlt/pgt ' an inline
    expressionlt/bodygtlt/htmlgt

49
ASP.NET Inline Script
  • In the above example the variable Message is
    defined in the script block at the top of the
    page. Since it is not defined within a subroutine
    it is globally accessible by other scripts on the
    page.
  • Within the body of the page the inline script lt
    Message"My message." gt assigns the string value
    to the variable.
  • Although inline scripts were the primary way of
    programming conventional ASP pages, they are
    seldom used under ASP.NET.

50
ASP.NET Inline Script
  • Also, inline expressions can appear inside lt
    ... gt symbols to show the value of a single
    variable or computational expression. The equal
    sign () indicates that the script is to display
    the value generated by the enclosed script. In
    the above example an inline expression is coded
    within the HTML portion of the page in order to
    display the value of variable Message that was
    assigned in the previous inline script. Because
    this embedded script is preceded by an equal
    sign, the value of the enclosed expression is
    displayed on the page
  • Here is HTML code that displays My message.

51
ASP.NET Inline Script
  • Script variables appearing in inline code and
    inline expressions must be declared as global
    variables and not be hidden within subroutines as
    local variables.
  • Inline expressions have their place in ASP.NET
    but not to the extent that they were used in
    classical ASP.

52
ASP.NET Hello.aspx
  • lthtmlgtltbodygt
  • lt dim i as integer gt
  • lt for i3 to 7 gt
  • ltfont size lt i gt gt
  • Hello ltbrgt
  • lt Next gt
  • lt for i3 to 7 gt
  • ltfont size lt (10-i) gt gt
  • Hello ltbrgt
  • lt Next gt
  • lt/bodygt lt/htmlgt

53
ASP.NET Form Submission
  • Dynamic Web pages are responsive to user needs
    and desires, generating and delivering the
    information requested. The primary mechanism
    through which users make requests to the server
    is a Web form. By clicking buttons, entering
    text, choosing from menus, and through other form
    controls appearing on the page, a user transmits
    requests to the server. The submitted form
    delivers this information to the server, which,
    in response, runs scripts to produce output and
    deliver it back to the user.

54
ASP.NET Form Submission
  • A fundamental principle under ASP.NET is that
    forms and their controls are server objects, not
    simply HTML elements. This principle is
    illustrated by the fact that the ltformgt tag and
    its enclosed controls contain the specification
    runat"server". As server controls, form fields
    are directly accessible by server scripts.
  • Web form controls are enclosed within a single
    ltformgt tag in the format shown below
  • ltform runat"server"gt  ...server controls and
    HTML codelt/formgt

55
ASP.NET Form Submission
  • The parameter runat"server" indicates that form
    processing takes place on the server and that
    enclosed controls are accessible by server
    scripts.
  • If you are familiar with conventional form
    processing, you notice some missing attributes.
    The action"url" parameter required for
    conventional forms is not needed, and is ignored
    if coded. The assumption is -- and the
    requirement is -- that form data are submitted to
    scripts on the same page as the form.

56
ASP.NET Form Submission
  • The POST method coded on conventional forms is
    also assumed and needs not be coded on server
    forms. A data transmission method needs to be
    specified only when using a GET method, unlikely
    when transmitting form data. Also optional is the
    name attribute. Form names are automatically
    assigned by ASP.NET for its own internal use.
  • It is important to remember that only one ltform
    runat"server"gt control can appear on a page. It
    is often convenient to code the opening tag
    immediately following the ltbodygt tag and the
    closing tag immediately preceding the lt/bodygt
    tag. In this fashion the entire page becomes a
    form within which server controls can be placed.

57
ASP.NET Input Controls
  • There are server control equivalents for all of
    the HTML form tags that appear on conventional
    forms, plus others. Some of these controls have
    the purpose of permitting user input into
    scripts, supplying data that the scripts process.
    The standard textbox, for example, coded as
  • ltinput type"text" name"MyTextField"/gtis
    replaced by the special server control
  • ltaspTextBox id"MyTextField" runat"server"/gt
  • The prefix asp identifies this as a server
    control, id"MyTextBox" provides a unique
    identifier for reference within scripts, and
    runat"server" makes the control accessible by
    server scripts.

58
ASP.NET Input Controls
  • All server controls are coded using XHTML
    notation. That is, all controls must have opening
    and closing tags. Certain controls (container
    tags) have an explicit pair of opening and
    closing tags others (empty tags) do not. These
    latter, single, tags are closed with a forward
    slash (/) to indicate that they serve as both the
    opening and closing tag.

59
ASP.NET Input Controls
  • Web Form Control Equivalent HTML Tag
  • ltaspTextbox/gt ltinput type"text"gt
  • lttextareagt...lt/textareagt
  • ltaspRadioButton/gtltaspRadioButtonListgt ltinput
    type"radio"gt
  • ltaspCheckBox/gtltaspCheckBoxListgt ltinput
    type"checkbox"gt
  • ltaspDropDownList/gtltaspListBoxgt ltselectgt...lt/s
    electgtOutput Controls

60
ASP.NET Input Controls
  • In conventional Web page development displayed
    information is hard-coded as text and graphics
    surrounded by HTML code to control layout and
    formatting. Under ASP.NET this method changes
    dramatically. In the extreme case, the only
    information or coding that appears on the page
    are server controls. These "output" controls
    serve as target areas for script output, with the
    script producing all text, graphics, and HTML
    formatting. It would not be unusual, for
    instance, for the HTML portion of a page to
    resemble the following, even on very complex
    pages
  • lthtmlgtltbodygtltaspLabel id"PageOutput"
    runat"server"/gtlt/bodygtlt/htmlgt

61
ASP.NET Input Controls
  • In this case, all page output, layout, and
    formatting are generated by scripts, with the
    ltaspLabelgt control serving as a target area for
    rendering the Web page. Most pages are not this
    minimal. They integrate standard HTML layout and
    formatting tags along with server controls for
    targeting text and graphic output. Still, much of
    the styling of the page comes from server
    scripts.
  • There are several server output controls that
    serve as targets for script output.

62
ASP.NET Input Controls
  • Web Form Control Equivalent HTML Tag
  • ltaspLabel/gt ltspangt...lt/spangt
  • ltaspPanelgt ltdivgt...lt/divgt
  • ltaspTablegt lttablegt
  • ltaspImage/gt ltimggt
  • Of course, it is a bit artificial to categorize
    some controls as purely "input" or "output"
    controls. An aspTextBox control, for example,
    serves both purposes. You can enter text for
    submission to a script, and it can be the target
    for script output. So, the classifications used
    here have very soft edges and indicate, very
    generally, the purpose of the controls.

63
ASP.NET Script Activation Controls
  • Scripts can be activated by user events
    surrounding the Web page. Certain controls, then,
    are provided for trapping those events and taking
    action on them. The primary example of a script
    activation control is the ltaspButtongt control.
    The user clicks the button a subroutine is
    called in response.
  • Script activation controls come packaged with
    event handlers to trap and take action on user
    events. The most common event is a mouse click on
    a button the most common event handler is the
    OnClick handler associated with the button.

64
ASP.NET Script Activation Controls
  • For example, the following button definition,
  • ltaspButton Text"Submit" _
  • onClick"Display_Output" runat"server"/gt
  • displays a button control with the label
    "Submit." It includes an OnClick event handler
    which makes the button sensitive to a mouse
    click. When the button is clicked, the event
    handler calls the Display_Output subprogram and
    that script is run.

65
ASP.NET Script Activation Controls
  • There are a number of server controls designed
    for the purpose of trapping user events and
    responding to them.
  • Web Form Control Equivalent HTML Tag
  • ltaspButton/gt ltinput type"button"gt
  • ltaspImageButton/gt ltinput type"image"gt
  • ltaspLinkButton/gt ltinput type"button"gt
  • ltaspHyperLink/gt lta hrefgt

66
ASP.NET Script Activation Controls
  • Script Activation EventsScripts can be run in
    response to Web page events as well as to user
    events. A key page event is the load event, which
    occurs when the page is first retrieved by the
    server in response to a URL request. At the same
    time, there are two conditions under which a page
    is loaded. On the one hand, it can be an initial
    page-load event -- the first time the page is
    retrieved in response to a URL request or, it
    can be a post-back event -- a reloading of the
    same page on form submission. These two page-load
    events can be, and often are, programmed
    separately.

67
ASP.NET Script Activation Controls
  • ltSCRIPT runat"server"gt Sub Page_Load()
  •    If Not Page.IsPostBack Then     --do this
    on initial page load   End If   --do this
    on every page load End Sublt/SCRIPTgt
  • In this example the Page_Load subroutine (this
    subprogram name is required) is run every time
    the page is loaded. It is run when the page is
    initially accessed through a URL request it is
    run when the user clicks a control to call a
    subroutine.

68
ASP.NET Info. Display Controls
  • There are three special controls that have no
    equivalence among standard form tags. These
    controls are unique to ASP.NET and are designed
    to ease and to automate the display of complex
    information. Most often these controls are used
    to display tables of data from databases. They
    are designed so that minimal coding is required
    to extract information from those tables and
    format it for display. We'll leave discussion of
    these controls for later.
  • Web Form Control Equivalent HTML Tag
  • ltaspRepeatergt (none)
  • ltaspDataGridgt (none)
  • ltaspDataListgt (none)

69
ASP.NET Passing Arguments
  • Whenever a server control calls a subroutine it
    must identify itself to the subroutine as the
    source object originating the call, and it can
    optionally supply the subroutine with data
    values, or event arguments, on the basis of which
    the subroutine carries out its processing. A
    standard way of coding subroutines to receive
    this information is in the following format

70
ASP.NET Passing Arguments
  • Sub Subprogram_Name (Src As Object, Args As
    EventArgs)  ...End SubThe above signature is
    the format of a subroutine called by a button
    click. Here, Src As Object is a reference to the
    control which called the subroutine. Args As
    EventArgs is a reference to any control
    information passed to the subroutine.
  • Different controls supply different arguments,
    and different properties of the arguments can be
    accessed for information about what type of
    processing should take place.

71
ASP.NET Passing Arguments
  • For subprograms that are not called by server
    controls, there may or may not be a need for an
    argument list.
  • It often depends on programmer coding preferences
    and practices. When one subprogram calls another
    subprogram, and no data need to be exchanged
    between the two, then no argument list is
    necessary.

72
ASP.NET Passing Arguments
  • Sub Button_Click (Src As Object, Args As
    EventArgs)  Do_ProcessingEnd SubSub
    Do_Processing  ...processing statementsEnd Sub
  • In this example, Button_Click is called by a
    server control, thereby requiring the standard
    argument list. This subprogram, in turn, calls
    Do_Processing, which does not contain an argument
    list because the calling subprogram does not pass
    any data to it.

73
ASP.NET Passing Arguments
  • There may be occasions, though, when an argument
    list is needed by internally called subprograms
  • Sub Button_Click (Src As Object, Args As
    EventArgs)  Dim MyDataValue As
    String  Do_Processing(MyDataValue)End SubSub
    Do_Processing (YourDataValue As
    String)  ...processing statementsEnd Sub

74
ASP.NET Passing Arguments
  • Here, the Button_Click subroutine encloses a
    local variable named MyDataValue. This data item
    is passed to subroutine Do_Processing in order
    for additional processing to take place on the
    item.
  • The Do_Processing subroutine, therefore, needs a
    way to receive the sent data. It does so by
    defining its own variable, YourDataValue, through
    which the passed value is received.
  • These data interrelationships between subprograms
    are coded in standard Visual Basic fashion.

75
ASP.NET Passing Arguments
  • An option to the explicit passing of values from
    one subroutine to another is to declare a global
    variable outside of both subroutines.
  • Each subroutine has access to the variable with
    no requirement for passing it.

76
ASP.NET Passing Arguments
  • '-- Declare global variable for access by all
    subroutines
  • Dim MyDataValue As String
  • Sub Button_Click(Src As Object, Args As
    EventArgs)
  • Do_Processing
  • End Sub
  • Sub Do_Processing
  • ...processing statements
  • End Sub
  • In the above example both subprograms have access
    to variable MyDataValue because is has been
    declared as a global variable. It is not
    necessary to pass this variable between
    subprograms. In general, though, variables should
    be declared locally and passed explicitly from
    one subprogram to another.

77
ASP.NET Calling Functions
  • Besides subprograms, functions can be called upon
    to perform processing. The difference is that
    functions return a data value to the calling
    element. It is often the case that a function is
    called from an inline script to supply a data
    value for display on the page. A simple example
    is the following
  • ltSCRIPT runat"server"gtFunction
    Get_Date  Return DateString()End
    Functionlt/SCRIPTgtThe current date is lt
    Get_Date gt

78
ASP.NET Calling Functions
  • The inline expression calls the Get_Date
    function. The function returns the current date
    which is substituted for the function call and is
    displayed on the page. This exact function is
    coded on this page to produce the following
    output.
  •      The current date is 04-07-2005.
  • Notice that this function does not have an
    argument list. No values are passed to the
    function the returned value (the current date)
    is embedded directly within the HTML code on this
    page.

79
ASP.NET Form Submission
  • Whenever a user event triggers a subroutine call
    a URL request is made to the server to retrieve
    and reload the current page. At the same time,
    information contained in the form coded on the
    page is submitted to the server and is made
    available to scripts coded on that page. Usually,
    that submitted information is processed by the
    subprogram identified in the event handler of the
    control that triggered the submission. But this
    need not be the case.
  • A control can trigger a form submission without
    identifying a subprogram.

80
ASP.NET Form Submission
  • For instance, the following button,
  • ltaspButton Text"Click My" runat"server"/gt
  • makes a URL request and submits the page for
    server processing however, it does not contain
    an event handler identifying a subroutine to
    handle the processing. In this case, Visual Basic
    statements contained in the Page_Load subroutine
    would have to handle the processing. Although
    this is not the normal way to handle ASP.NET
    processing, it offers the conventional ASP mode
    of form submission.
Write a Comment
User Comments (0)
About PowerShow.com