Slide 11.4: Adding simple code to a page (cont.)
Slide 11.6: Creating a dynamic checkbox list
Home

Adding Simple Code to a Page (Cont.)

Using Code Render Blocks (Cont.)
While <% %> code blocks provide a powerful way to custom manipulate the text output returned from an ASP.NET page, they do not provide a clean HTML programming model. As the previous sample illustrates, developers using only <% %> code blocks must custom manage page state between round trips and custom interpret posted values.

  • The Request object retrieves the values that the client browser passed to the server during an HTTP request.

  • The Request.QueryString collection retrieves the values of the variables in the HTTP query string, which is specified by the values following the question mark ‘?’. For example, the following anchor tag generates a variable named string with the value “a sample.”
      <a href="example?string=a sample">string sample</a>
    Query strings are also generated by sending a form or by a user typing a query into the address box of the browser.

  • The HttpUtility class provides methods for encoding and decoding URLs when processing web requests.

  • The HttpUtility.HtmlEncode method converts a string to an HTML-encoded string. HTML encoding makes sure that text is displayed correctly in the browser and not interpreted by the browser as HTML. For example, if a text string contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as the opening or closing bracket of an HTML tag. When the characters are HTML encoded, they are converted to the strings &lt; and &gt;, which causes the browser to display the less than sign and greater than sign correctly.

  • The Nothing keyword represents the default value of any data type. Assigning Nothing to a variable sets it to the default value for its declared type.

The code illustrates a limitation in classic ASP: The code block has to be placed where you want the output to appear. With classic ASP it is impossible to separate executable code from the HTML itself. This makes the page difficult to read, and difficult to maintain.