- Day 3: Using Web Forms
- Performing Basic State Management in Web Applications
- Using Cookies
- Using Hidden Fields and Query Strings
- Working with the Session Object
- Working with the Application Object
- Setting Up Global Objects with the global.asax File
- Configuring the Application
Using Hidden Fields and Query Strings
You also can store small amounts of information on the client by using hidden fields. Hidden fields are HTML elements, similar to text boxes, where you can store strings. Web browsers don't display hidden fields in page output. However, when you use a hidden field within an HTML form, the contents are submitted back to your program.
To use hidden fields, use a code line that looks similar to the following within a <form> area:
<input type="hidden" value="Value That You Need to Store" id="KeyName">
You can extract the hidden field's value by using the Params collection in the Request object, using a code line like this:
Dim strValue As HttpCookie = Request.Params("KeyName")
As you will see in the following lessons, hidden fields play a much smaller role in ASP.NET Web programming than in other technologies, such as the older ASP. This is true because ASP.NET Web controls keep track of their own state and because the Page class contains a ViewState collection in which you can store string data easily.
You can also use query strings to pass parameters from page to page. Query strings are bits of information appended to an URL, as in the following line:
<a href="ShowSales?Dept=Mfg&Year=2001>Maufacturing Sales</a>
Listings 3.4 and 3.5 show examples of one page that passes a parameter to another page using a query string.
Listing 3.4 ShowSales.htm: Passing a Query String to SalesFigures.aspx
<html> <body> <h2>Please select one of the links below for Widgets Inc quarterly sales figures</h2> <a href="salesfigures.aspx?Department=Manufacturing">Manufacturing</a> <br> <a href="salesfigures.aspx?Department=RD">Research & Development</a>
Listing 3.5 SalesFigures.aspx: Showing Different Sales Depending on a Query String
<%@ Page Language="VB" %> <html> <body> <h2>Sales figures for Widgets Inc</h2> <% WriteSales() %> </body> </html> <script runat="server"> Sub WriteSales() if Request.Params("Department") = "Manufacturing" Then Response.Write("<b>Manufacturing Quarterly Sales</b><br>") Response.Write("Quarter 1 Sales: $24M<br>") Response.Write("Quarter 2 Sales: $34M<br>") Response.Write("Quarter 3 Sales: $12M<br>") End If if Request.Params("Department") = "RD" Then Response.Write("<b>Research and Development Quarterly Sales</b><br>") Response.Write("Quarter 1 Sales: $2M<br>") Response.Write("Quarter 2 Sales: $3M<br>") Response.Write("Quarter 3 Sales: $1M<br>") End If End Sub </script>