IsPostBack with Response.Redirect to same page

D

DotNetGruven

I've got a page that does a search... There is an ImageButton on it that
allows the user to reset the search.

The Click Handler for the button clears out the Data and then redirects to
the same page with Response.Redirect("Search.Aspx", true);

Sometimes the code in Page_Load within the IsPostBack part of the if
statement runs and sometimes it doesn't. IN other words, sometimes
IsPostBack is true and sometimes it is not!

How can I reliably post back to the same page from within an event Handler
and get a fresh post?

I've tried Response.Redirect with both true and false as the second
parameter and Server.Transfer.

One final note, the page in question is the default page for the directory
set via the IIS snapin. (not sure if this is important)

TIA
geo
 
E

Eric Lawrence [MSFT]

I don't think IsPostBack should ever be true if the page is being hit from a
Response.Redirect. Response.Redirect sends back a HTTP/302 to the browser.
When receiving a HTTP/302 in response to a HTTP POST, a properly behaving
client (e.g. IE) will perform a HTTP GET on the new URL and thus will not
POST the form data to the new page.
How can I reliably post back to the same page from within an event Handler
and get a fresh post?

Rather than using Response.Redirect, why not just have the event handler for
the reset button clear all of the fields?

--
Thanks,

Eric Lawrence
Program Manager
Assistance and Worldwide Services

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Thanks a lot for Eric's informative suggestions.

Hi Geo,

As for using the Response.Redirect, I agree with Eric that this will cause
a new request for the new specified url. So the "IsPostBack" property of
that page should be "false". As you mentioned that the problem page is set
as the "default page" of the certain IIS virtual folder, I do think it is
the potential cause fo the problem. I'm not sure on the detailed codes of
the page and content. But I suggest you tested on a simple clear page:
1. add button and in its click post back event, use response.redirect to
redirect to itself.

2. add breakpoints into the page_load and the click event.

Use F5 debug to trace this page. See whether the IsPostBack property is
always "false" after response.redirect. I always think it should be
"false".

In addition, as Eric has mentioned, why don't you just clear the
dataContents in the button's post back event rather than use
response.redirect?

Please check out the above suggestions. If you have any further questions,
please feel free to post here.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
M

Mark

I have a similar desire to have Response.Redirect actually cause a
post back. Here is why

private void Page_Load(object sender, System.EventArgs e)
{
LoadContentControl();

if (!Page.IsPostBack)
{
BindTabs();
BindContent();
}
}


The now LoadContentControl() dynamically (this.LoadControl) loads a
specific user control (.ascx) depending on the Selected Tab (which is
stored in a viewstate variable) that i have on the page. By default
(when no tab is orignially selected ie. on a first time visit to the
page) it loads a specified user control. That works great. Now when
someone clicks on one of the tabs a different user control should
load. Effectively, the tab_clicked event fires, the viewstate variable
specifying the different control changes correctly, and that event
calls the LoadContentControl again. The new Control loads fine EXCEPT
that the ViewState is still that for the old control. I know this
because when the user tries to fire an event on the newer user
control, the event isn't caught. However the next time the user tries
to fire an event it works. I assume that this is because the first
time, dynamic user control was loaded after the page load but the
second time it is being loaded in the Page_Load and not after that
phase. So what i suggest is that when the user clicks on a new tab,
the tab event code changes the viewstate variable and asks the page to
reload immidiately, or to reinitialize. Then the correct control would
be loaded early enough to get into the ViewState

Any ideas

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top