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.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 15 of 15.
-
server.execute with variable
2004-04-27 21:59:49 broch [Reply | View]
I want to do something like:
string name = username;
server.execute("userdetails.aspx?username");
Is it possible and what is the exact syntax in c#?
-
and what about redirecting to frames?
2004-02-13 12:34:09 mansyno [Reply | View]
all this is very nice
but lets add frames to it
what if i have two frames set in the window and i want the element on one frame to couse another (dynamicly decided at run time) page to reload itself in the upper frame
i need data from the buttom frame to be availble to the oage that loads in the upper frame
any ideas?
and i know ll the bad things said about frames
but i must use frames couse my customer wantes this funcntionality
hope you have ideas
daniel
-
Server.Execute() method
2004-01-27 11:58:58 kerno [Reply | View]
Zour article is excelent but:
I test the example you described in the paragraph above Server.Execute. My test failed with the error message: The View state is invalid for this page and might be corrupted. Where is the problem?
-
Learning by examples and explainations?
2003-08-19 15:10:16 anonymous2 [Reply | View]
I believe 90% of programs are visuals. Mean they learn quick by examples. I have a form on step1.aspx
When I click the button I do a server.transfer (step2.aspx, true)
If I use click the previous button I have in step2.aspx it calls a
server.transfer (step1.aspx, txt). In my page_load of step1.aspx the following
is declared:
string Email = (string)Session["txtEmail"];
My definition in step1.aspx
<asp:TextBox id="txtEmail" runat="server" width="150px" maxlength="60"></asp:TextBox>
Now if I want to populate or set (keep) the textbox value to what
the user entered, what should I do. I can see the value of Email, by doing a Response.Write(Email);
Now, I just have to make sure that the textbox is not empty.
I tried the following:
<script language="C#" runat="server">
// Define String Email
public String Email {
get {
return txtEmail.Text;
}
set {
txtEmail.Text = value;
}
}
and
public String Email {
get {
return txtEmail.Text;
}
Still I get nothing? What am I doing wrong? I appreciate any assistance.
I am finally learning slowly asp.net.
Thank you.
-
great but how with dropdownlist and button
2003-06-30 07:59:39 anonymous2 [Reply | View]
I've had no luck searching for an example of how to do a simple external link using a dropdownlist and a button with asp.net. Can anyone help? aforonda@yahoo.com
thanks,
Nico -
how to call one frame to another
2006-07-31 04:01:01 nath123 [Reply | View]
i am omkar,In one from i am creating three frame,one frame from logo,2nd for timer and 3rd for button,when button is pressed then timer will stop.
i am using c# asp.net,give me suggestion how to call one frame to another and how to solve this problem,
please reply me i am waiting to your respose,its very urgent..ok
thanks -
how to send
2006-07-31 03:59:19 nath123 [Reply | View]
i am omkar,In one from i am creating three frame,one frame from logo,2nd for timer and 3rd for button,when button is pressed then timer will stop.
i am using c# asp.net,give me suggestion how to call one frame to another and how to solve this problem,
please reply me i am waiting to your respose,its very urgent..ok
thanks
-
DON'T disable EnableViewStateMac!!!
2003-04-20 06:28:41 anonymous2 [Reply | View]
Setting EnableViewStateMac to 'false' (as described under section Server.Transfer) is not secure, for a secure solution please see,
http://support.microsoft.com/default.aspx?scid=kb;EN-US;316920
and
design pattern shown in...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconpassingservercontrolvaluesbetweenpages.asp
-
Clarifications on when to use Response.Redirect vs. Response.Execute/Transfer
2003-04-14 14:57:23 prosser [Reply | View]
In most cases use of Response.Redirect or Response.Transfer boils down to whether you have control of the destination page and whether the user should "know" about the transfer. Performance, although differing between the methods, is not usually the key concern for me when I make this decision.
For example, when I worked for a certain large website, we would often get links from partner sites, and link out to partner sites. Since there was money involved in tracking those clicks, there had to be a tracking mechanism or "funnel" to make that sort of thing feasible. On incoming hits we could use Response.Transfer (we didn't--more on that later), since we controlled the destination page. On outgoing clicks we had to use Response.Redirect (there's no place to transfer to, so you must HTTP/1.0 302 redirect the request).
Even if you control the destination page, another consideration is the URL that shows up on the user's address bar. If you use Response.Transfer/Execute, the pre-transfer URL will appear in their address bar; Response.Redirect will result in the post-transfer URL appearing in their address bar. Now you may not really care that much, but in some cases you definitely should. For example, if you pay for click-throughs from a partner, do you REALLY want to pay them every time a user hits refresh on that page? How about when a user bookmarks the page? Even though there is a (small) performance gain for a single use of Response.Transfer it is balanced against the fact that if a user bookmarks the destination URL, they are actually bookmarking the pre-transfer URL, and so your server must keep transfering them every time ad infinitum (perhaps earning money for a partner each time as well).
That having been said, there's definitely a place for Server.Transfer. I use it after form validations in a "wizard" scenario, since you want to defeat bookmarks there anyhow and to keep the business logic where it makes sense--on the page that hosts the form the user just filled out. Call me silly but I don't care for pages where I have to validate a post from a previous page... the disconnect bothers me.
Just my $0.02,
Peter
-
What different between window.open and hyperlink ? Which is good performance
2003-04-13 16:34:45 anonymous2 [Reply | View]
-
What different between window.open and hyperlink ? Which is good performance
2003-04-14 14:45:06 prosser [Reply | View]
Window.open is a client-side javascript method and has no bearing on performance, at least not with respect to perceived perf by the client. Since that's a purely client-side operation, it really is tangential (and that's stretching it) to any discussion of Response.Redirect/Execute/Transfer.
Although there are times when a window.open should be used, most times it is simpler just to use a normal hyperlink. Exceptions are when you need to navigate via timed event, selection in a dropdown, etc. Keep in mind that if you do an automatic redirection when the user isn't expecting to be redirected that's a bad thing in most cases. Users just hate unpredictable (to them) interfaces.
Just my $0.02...







"The viewstate is invalid for this page and might be corrupted"
Thanks for sharing.. errr... might want to TEST the code before you write an article on it.
(Yes, I changed the EnableViewStateMac to false in the page directive and I'm on 1.1)
;)