Slide 10.e: Adding simple code to a page
Slide 10.g: Adding simple code to a page (cont.)
Home

Adding Simple Code to a Page (Cont.)


ASP.NET page developers can utilize <% %> code blocks to dynamically modify HTML output much as they can today with ASP. For example, the following sample demonstrates how <% %> code blocks can be used to interpret results posted back from a client.

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <body>
  <center>
   <form action="intro3_vb.aspx">
    <h3> Name: <input name="Name" type=text
    value="<%=HttpUtility.HtmlEncode(Request.QueryString("Name"))%>">
     Category:
     <select name="Category" size=1>
      <%
       Dim I As Integer
       Dim Values(2) As String
       Values(0) = "psychology"
       Values(1) = "business"
       Values(2) = "popular_comp"
 
       For I = 0 To Values.Length - 1 
      %>
      <% If (Request.QueryString("Category") = Values(i)) %> 
      <option selected>
      <% Else %>
      <option>
       <% End If %>
       <%=Values(i)%>
      </option>
      <% Next %> 
    </h3>
    <input type=submit name="Lookup" value="Lookup">
    <p>
     <% If (Not Request.QueryString("Lookup") = Nothing) %>
     Hi <%=HttpUtility.HtmlEncode(Request.QueryString("Name")) %>,
     you selected:
     <%=HttpUtility.HtmlEncode(Request.QueryString("Category")) %>
     <% End If %>
    </p>
   </form>
  </center>
 </body>
</html>