Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Working with multiple pages in ASP .NET?

ASP .NET always posts forms back to the same web page (ie. does post-back).

Now, the problem is real, complex web sites need multiple pages.


For example, let's imagine a web mail.

You are in the inbox page. You click on the settings button. That should get you to a separate settings page. Now you edit the settings, and click submit. Now you should be again in the inbox page.

The ability of displaying several web pages and to navigate from page to page is critical to any complex dynamic web site.


Since ASP .NET only does post-back, how can I write a complex site with several pages and navigation from page to page?


I have searched the web a lot, and the only solution I found was to put the content of every page on a panel, and then show and hide panels as needed.

This is a horrible solution. Why? Because all my source code, event handlers, etc, will be crammed into one source file.

For a small, 2 or 3 page project this is ok, but for a project containing, let's say, 50 pages, this will be a nightmare, and I would revert to classic ASP.


So, to repeat the question, how do I move from page to page in ASP .NET?

How do I implement a multiple-pages site, in spite of the fact that ASP .NET only does post-back?


Thank you!

Jake
Wednesday, August 27, 2003

Use Server.Transfer with the second parameter as True.

Here is a fresh article on this
http://www.dotnetbips.com/displayarticle.aspx?id=183

Ram
Wednesday, August 27, 2003

I use a little smattering of javascript to alter the forms action.

Nice and simple that, and saves putting bouncing code all over your c# files. It's like goto s never went away!

Colin
Wednesday, August 27, 2003

"ASP .NET always posts forms back to the same web page (ie. does post-back)."

This is an over-simplification. For example, the following code generates a link that doesn't post back:

HyperLink link = new HyperLink();
link.NavigateUrl = "somepage.aspx?foo=bar";
Controls.Add(link);

Okay, now assuming that you really want to do something during a postback, and then when you're done move onto another page. Server.Redirect() is your friend.

Brad Wilson (dotnetguy.techieswithcats.com)
Wednesday, August 27, 2003

The statement "ASP .NET always posts forms back to the same web page" is incorrect.  ASP.NET always posts forms back to the same web page if they have runat=server set.  You can have as many forms as you'd like as long as at most one of them has runat=server. 

This makes sense if you think about it.  If you are using a desktop windowing system (Win32, MFC, classic VB, WinForms, whatever), you don't create a Settings dialog by specifying with some property that the "Settings" button or menu item gets forwarded to the Settings dialog.  You respond to the event and create a new Settings dialog.  ASP.NET Web Forms are intended to be a similar abstraction for web-based applications. 

So for multiple pages, you have a few choices:

1. Transfer control to a different URL using Server.Transfer or Response.Redirect during postback or just a simple hyperlink.  These should work fine for the Settings example since you probably won't have much state to transfer to the new page.  This is somewhat analogous to creating a new dialog/form in response to an event in a windowing system. 

2. Put each different "page" in its own user control.  Stick all of the user controls on the main page and toggle the visibility as needed.  This is somewhat analogous to a tab control in a windowing system. 

3. Put functionality that switches to a different page in a form that doesn't have runat=server.  This would be useful for something like a search box on a web page.  You create a form with action=search.aspx and a text box.  In search.aspx, you use the traditional Request object methods to retrieve the text box contents. 

SomeBody
Wednesday, August 27, 2003

The web user control approach can be refined by leting the host page load the control it needs instead of having it to decide which ones to hide/show.

    string controlFilename = DeduceControlFilenameFromQueryString();
    placeholder.Controls.Add(this.LoadControl(controlFilename);

Thomas Eyde
Saturday, August 30, 2003

*  Recent Topics

*  Fog Creek Home