Managing ASP.NET Navigation
Pages: 1, 2
Server.Execute
The Server.Execute method allows the current ASPX page to execute a
specified ASPX page on the same web server. After the specified ASPX page is
executed, the control transfers back to the original page from which the
Server.Execute method was called. This technique of page navigation is
analogous to making a function call to an ASPX page. The called ASPX page has
access to the form and query string collections of the calling page, and thus you
need to set the EnableViewStateMac attribute of the Page directive to False on
the executed page.
By default, the output of the executed page is added to the current response
stream. This method also has an overloaded version in which the output of the
redirected page can be fetched in a TextWriter object (or one of its children,
such as a StringWriter object) instead of added directly to the response stream.
This helps you to control where to place the output in the original page.
To see how this works, create a Web Form in a test ASP.NET application and
place a Button control (Button1) and a Literal control (Literal1) on the Web
Form. Switch to code view and add an Imports statement for the System.IO
namespace. Then add code to execute when the user clicks the button:
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim sw As StringWriter = New StringWriter()
Server.Execute("WebForm2.aspx", sw)
Literal1.Text = sw.ToString()
End Sub
Now create a second Web Form in the same application, WebForm2.aspx. Switch
to the HTML view of this second Web Form and modify its Page directive to disable
ViewState checking:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb"
Inherits="Navigate.WebForm2" EnableViewStateMac="false"%>
Switch back to design view and add some controls to the second Web Form.
Now set the first Web Form as the default page and start the application. Click
the button, and the controls from WebForm2 will be displayed in the area of
WebForm1 where you placed the Literal control, as shown in Figure 1. You'll
note from the URL and page title that the browser is still displaying
WebForm1.
|
|
There's one more thing to be aware of when you use the Server.Transfer or
Server.Execute methods to navigate: the ultimate page may not be valid HTML.
That's because the response to the client will contain multiple <html> and
<body> tags, among other tags. Internet Explorer seems to tolerate
this situation just fine, but you may want to test the results carefully if your
users prefer a different browser.
Decisions, Decisions
So, given these choices for navigating from page to page, how do you select the appropriate one for your application? Here are some things to think about:
Hyperlinks are appropriate when you want the end user to control when navigation is performed, or to choose where to go.
To control the user's destination, but let them decide when to get there, use a Web Server HyperLink control whose
NavigateUrlproperty is dynamically set.Use
Response.Redirectto connect to resources outside of the web server where your page is hosted.Use
Response.Redirectto connect to non-ASPX resources such as HTML pages.Use
Response.Redirectif you need to preserve a query string as an explicit part of the URL.When you want to transfer control to an ASPX page residing on the same web server, you should use
Server.Transferinstead ofResponse.RedirectbecauseServer.Transferwill avoid the unnecessary round trip and provide better performance and a better user experience.To capture the output from an ASPX page and display it at a specified location on another ASPX page, use
Server.Execute.If valid HTML output is essential, use
Response.Redirectinstead of either theServer.TransferorServer.Executemethods.
Mike Gunderloy is the lead developer for Larkware and author of numerous books and articles on programming topics.
Return to ONDotNet.com.
- Trackback from http://vertigoblogs/petshop/archive/2005/08/23/1424.aspx
Tricks for Improving the Performance of your ASP.NET Web Applications
2005-08-23 00:04:49 [View]
-
Looks cool, but does not work.
2004-06-24 18:21:41 unleashed [View]
-
Looks cool, but does not work.
2005-09-03 08:22:48 cwefong [View]
-
server.execute with variable
2004-04-27 21:59:49 broch [View]
-
and what about redirecting to frames?
2004-02-13 12:34:09 mansyno [View]
-
Server.Execute() method
2004-01-27 11:58:58 kerno [View]
- Trackback from http://weblogs.asp.net/firoz/posts/0.aspx
Managing ASP.NET Navigation
2003-12-21 03:45:12 [View]
-
Learning by examples and explainations?
2003-08-19 15:10:16 anonymous2 [View]
-
great but how with dropdownlist and button
2003-06-30 07:59:39 anonymous2 [View]
-
great but how with dropdownlist and button
2007-03-08 21:05:22 hellomorning [View]
-
how to call one frame to another
2006-07-31 04:01:01 nath123 [View]
-
how to send
2006-07-31 03:59:19 nath123 [View]
-
great but how with dropdownlist and button
2003-07-05 11:38:23 santosc5024 [View]
-
DON'T disable EnableViewStateMac!!!
2003-04-20 06:28:41 anonymous2 [View]
-
Clarifications on when to use Response.Redirect vs. Response.Execute/Transfer
2003-04-14 14:57:23 prosser [View]
-
What different between window.open and hyperlink ? Which is good performance
2003-04-13 16:34:45 anonymous2 [View]
-
What different between window.open and hyperlink ? Which is good performance
2003-04-14 14:45:06 prosser [View]


