03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

280 x CHAPTER 7 NAVIGATION<br />

object provide you with <strong>in</strong>formation about the server the page is runn<strong>in</strong>g on. You can use it to get<br />

<strong>in</strong>formation about the server name, its IP address, <strong>and</strong> so on. One of its methods is Transfer, which<br />

performs a server-side transfer.<br />

You can use Server.Transfer only to redirect to other pages with<strong>in</strong> your site. You cannot use it to<br />

send the user to pages on different doma<strong>in</strong>s. If you try to do so, the <strong>ASP</strong>.<strong>NET</strong> run time throws an<br />

error.<br />

To see the difference between Response.Redirect <strong>and</strong> Server.Transfer, the follow<strong>in</strong>g exercise<br />

shows you how to change the page Source.aspx to perform a Server.Transfer operation.<br />

TRY IT OUT<br />

Server-Side Redirect<strong>in</strong>g<br />

It’s easy to change the redirect code so it transfers the user to another page. All you need to do is<br />

replace Response.Redirect with Server.Transfer as demonstrated <strong>in</strong> this exercise.<br />

1. Open the Code Beh<strong>in</strong>d of Source.aspx <strong>and</strong> replace the l<strong>in</strong>e with Response.Redirect with the<br />

follow<strong>in</strong>g l<strong>in</strong>e:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Protected Sub Page_Load(sender As Object, e As EventArgs) H<strong>and</strong>les Me.Load<br />

Server.Transfer("Target.aspx?Test=SomeValue")<br />

End Sub<br />

C#<br />

protected void Page_Load(object sender, EventArgs e)<br />

{<br />

Server.Transfer("Target.aspx?Test=SomeValue");<br />

}<br />

2. Save the changes <strong>and</strong> then press Ctrl+F5 to open Source.aspx <strong>in</strong> the browser (see Figure 7-13).<br />

FIGURE 7-13<br />

The Label control displays the query str<strong>in</strong>g values that were sent from Source.aspx to Target<br />

.aspx, demonstrat<strong>in</strong>g the fact that you are really view<strong>in</strong>g the output of the Target.aspx page.<br />

However, the browser’s address bar is left unmodified <strong>and</strong> still shows Source.aspx, hid<strong>in</strong>g the<br />

new page name <strong>and</strong> query str<strong>in</strong>g values from the user.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!