Performing Page Navigation (Scenario 2)


Client-side page redirects or navigations can also be initiated from the server by an ASP.NET page developer by calling the Response.Redirect method. The following sample demonstrates how to use the Redirect method to pass parameters to another target page. The object Response sends output to the client and its method Response.Redirect(URL) causes the browser to redirect the client from controls6_cs.aspx to a different URL at Controls_NavigationTarget_cs.aspx with a query_string of “name=name.txt”, where name.text is the input value of the TextBox.


Controls6_cs.aspx

Web
<%@ Page Language="C#" AutoEventWireup="true"
  CodeBehind="Controls6_cs.aspx.cs"
  Inherits="WebApplication13.Controls6_cs" %>

<script language="C#" runat="server">
 protected void EnterBtn_Click( object sender, EventArgs e ) {
  if ( Name.Text != "" )
   Response.Redirect( "Controls_NavigationTarget_cs.aspx?Name=" +
     Server.HtmlEncode( Name.Text ) );
  else
   Message.Text = "Hey! Please enter your name in the textbox!";
 }
</script> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
 <body>
  <h3><font face="Verdana"> 
   Performing Page Navigation (Scenario 2)
  </font></h3>
  This sample demonstrates how to navigate to a new
  page from within a <asp:button> click event,
  passing a <asp:textbox> value as a querystring
  argument (validating first that the a legal textbox
  value has been specified).
  <hr />
  <form runat="server">
   <font face="Verdana">
    Please enter your name:
    <asp:textbox id="Name" runat="server" />
    <asp:button text="Enter" Onclick="EnterBtn_Click" _
      runat="server" />
    <asp:label id="Message" forecolor="red" _
      font-bold="true" runat="server" />
   </font>
  </form>
 </body>
</html>