Capt. Horatio T.P. Webb
Client to Server Object References
Postbacks (VB.NET and C#)

Parks -- SPRING 2012
Last Updated 7AM 5/5/2012

There are several ways to get data from the client to the server. Here are your options:

  1. An HTML page sends data to *.asp page

    Client data contained in an HTML form can be retrieved in an asp program using:

    local variable = Request.Form ("name attribute used in the HTML") -- if the form has METHOD="POST"

    OR

    local variable = Request.Querystring ("name attribute used in the HTML") -- if the form has METHOD="GET"

    You can also access the form or querystring element values by using:

    local variable = Request.Querystring (integer value) -- if the form has METHOD="GET"

    OR

    local variable = Request.Form (integer value) -- if the form has METHOD="POST"

    You can thus loop through the elements (the integer value number above must begin at zero). You must be careful that the order of elements corresponds to their sequential occurrence in the HTML form.

  2. An HTML page sends data to *.aspx page

    An Client side HTML Page

    Nothing special here. It is an HTML page (*.htm or *.html) with just a form with some named HTML objects which is submitted to the *.aspx Program

    The server side *.aspx Page

    This page has all the code in the Page_load subroutine (there is NO postback)
    (it is

    <HTML>
    <HEAD>
    maybe a clientside validation script here
    </HEAD>
    <BODY>
    <form action="some_url/some_path/some_program.aspx">
          METHOD="POST">
    ...
    <input type="text" name="m1"...>
    <input type="text" name="m2"...>
    <input type="text" name="m3"...>
    <input type="text" name="m4"...>
    <input type="submit">
    ...
    </BODY>
    </HTML>
    <%@ Page Language = "VB"%>
    ...
    <script runat="server">

    Sub Page_load (ByVal sender As Object, ByVal e As System.EventArgs)
       Dim b1 as String
       Dim b2 as String
       Dim b3 as String
       Dim b4 as String
       b1=Request.Form("m1")
       b2=Request.Form("m2")
       b3=Request.Form("m3")
       b4=Request.Form("m4")
    ...
    End Sub


    Alternatively you can roll the data into an array like this:

       Dim b(5) As String
       Dim i as Integer
    ...
       for i=1 to 4
          b(i)=Request.Form("m"+cstr(i) )
       next

     

     

    If you were to use METHOD="GET"

     

     

    Instead of:

    ...=Request.Form(some HTML object name string)

    you would use:

    ...=Request.QueryString(some HTML object name string)

     

    In general the server-side program retrieves the the HTML object value by name. For example:

    some local variable = request.form("fred13")

    Since the value inside the parenthesis of the request.form (or request.querystring) is just a string, we can rogrammaticallty create the string that holds the name of the HTML object(s). Suppose an client-side HTML form has 20 elements named "fred1" through "fred20". We can load all these values into a server-side array using the following loop that constructs the element names as strings:

    .
    .
    .
    dim Array fred_value (20)
    for i=1 to 20
    &nsbp;   fred_value(i)=Request.form("fred"+ cstr(i) )
    next .
    .
    .
    We will exploit this characteristics in some of or assignments.

    For VB.NET the page would appear as:

    <%@ Page Language="VB" %>
    ...
    Sub Page_load(ByVal sender As Object, ByVal e As System.EventArgs)

       if NOT isPostBack then
          Dim b1 as String
          Dim b2 as String
          Dim b3 as String
          Dim b4 as String
          b1=m1.Text
          b2=m2.Text
          b3=m3.Text
          b4=m4.Text
    .
    .
    .
    End Sub
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    ...
    <form id="form1" runat="server">
    ...
    <asp:TextBox id="m1" MaxLength=... Runat="server"></asp:TextBox>
    <asp:TextBox id="m2" MaxLength=... Runat="server"></asp:TextBox>
    <asp:TextBox id="m3" MaxLength=... Runat="server"></asp:TextBox>
    <asp:TextBox id="m4" MaxLength=... Runat="server"></asp:TextBox>
    ...
    </form>
    </body>
    </html>