Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

ASP .NET redirect...

I am teaching myself vb .NET and ASP .NET with vb, and am hopeful to be able to ask a few questions here and hopefully gain enough knowledge to answer other people's questions sometime thanks in advance for any help!

My first question is regarding an asp.NET Web Form.  I wish to have a user click a button, and have the page reload itself upon the completion of that event handler.  What I have done is included the following line of code as the last executable statement in the button's OnClick event handler:


Where Form1.aspx is the name of the form itself.  This accomplished exactly what I wanted, but is there a way to do the same thing without hard-coding the web form's name?  I have been playing around with different classes and their properties, but cannot get anything else to work.  I figure the page has to somehow know what itself is called, but I can't figure out how reference it.  Any help would be much appreciated!!!


Keith Savage
Tuesday, October 28, 2003

If you look at the top of the ASPX code, you will see something like this:

<%@ Page language="c#" Codebehind="my_page.aspx.cs" AutoEventWireup="false" Inherits="MyProject.WebForms.MyPage" %>

Add trace="true" to this tag -- this will give you a whole load of useful debugging information at the bottom of the page. It gives you all of the Request.ServerVariables collection and one of those I'm pretty sure is the current URL.

You can then reference this in your code behind. E.g.


Marcus Greenwood
Wednesday, October 29, 2003


Redirecting to the same page after an onclick event seems like a strange thing to do. Perhaps you have a special case but it seems like you need reinvestigate the page lifecycle in ASP.NET.

If you could post what exactly you're trying to do I might be able to give you a better alternative.

Wednesday, October 29, 2003

Marcus, thanks for the tip.  I will try that out.

Paul, the page displays the results of a Select statement in a data grid, and then below it a form which the user can submit an additional record.  Upon this submission, I want the data set to include the added record, and then leave the form blank for another record to be added.  I would certainly be open to alternative ways to acomplish the same thing.

Many thanks to both of you.

Wednesday, October 29, 2003

Redirecting to the same page after an onclick event seems like a strange thing to do. Perhaps you have a special case but it seems like you need reinvestigate the page lifecycle in ASP.NET.

Redirecting to the same page clears the POST data allowing a user-initiated refresh to actually refresh the page rather than reposting the data or popping up a message box about a repost.  This helps to prevent duplicate operations that are almost never what the user intended (for example, posting the same reply to a thread twice).  It also allows the UI to behave properly since pressing the refresh key usually means the user wishes to refresh the current page, not repost form data (for example, after deleting an order on a page listing orders, pressing the refresh key should refresh the order listing, not attempt to delete the order again).  As far as I'm concerned, almost every postback operation in ASP.NET should end with a Response.Redirect. 

This is the thing that annoys me the most about ASP.NET.  By default, you end up with a crappy interface.  Not only is the back button broken as described above but the Enter key as a default form action is broken since you can only have one server-side form per page.  For example, go to the forums, enter some text in the search field on the left and hit Enter.  Instead of searching, it tries to log you in.  What were they thinking???

To answer the originally question, there are a variety of different ways to get at the URL through the Request object (besides ServerVariables).  For example:


There are some subtle differences in meaning between the different ways (for example, preserving query string arguments or not) but in most cases any will get the job done. 

Wednesday, October 29, 2003

I agree with SomeBody.

I've been down this path a lot, and redirecting to the same page after a modifying POST operation (heck, after any POST operation) just plain makes good sense from the browser point of view. Add in the relative difficulty you can have with the control lifecycle issues (two sets of control trees, potentially, during a POSTBACK that displays results), and it just weighs more in favor of the redirect.

Brad Wilson (
Wednesday, October 29, 2003

SomeBody, Brad:

Interesting points. I'll have to investigate that.

Learn something new everday I guess.

Wednesday, October 29, 2003

Thank you to everyone...using properties of the request object worked like a charm!


Thursday, October 30, 2003

*  Recent Topics

*  Fog Creek Home