Cross-page postback: Prevent Response.Redirect() being called from within PreviousPage
<p>I'm trying to create a search form for an internal web application, which will appear on most pages. I put the search form into an ascx user control (SearchPanel.ascx), and configured the Search button with PostBackUrl="Default.aspx". I've also created a public event handler on the user control, which fires when the Search button is clicked (so that I can handle searches that are initiated directly from Default.aspx).</p>

<p>Searching while viewing Default.aspx works fine, via the event handler in SearchPanel.ascx. When I try to search from a different page hosting the user control, the browser posts to Default.aspx, but this.PreviousPage is null. Thus I can't check for IsCrossPagePostBack, nor can I use PreviousPage.FindControl("Search") to access the public properties of the search form (text and a couple of checkboxes).</p>

<p>Is there something else I have to do in order to handle a cross-page postback initiated from a user control?</p>

<p><strong>Update:</strong></p>

<p>If I break the debugger during Page_Load and examine the Request.Form collection, I can see the form values being passed from the original page, along with a __PREVIOUSPAGE value that seems to be encrypted/encoded like ViewState.</p>

<p>When I step through Page_Load with the debugger, the results are rather weird. If I step-into on the line where it checks if PreviousPage != null, I can see it instantiating the page where the request came from. Once it's finished, it returns to the beginning of Page_Load in Default.aspx, and runs it again. This time, PreviousPage is simply null.</p>

<p>I must be doing something stupid, but so far I don't know what it is.</p>

<p><strong>The Plot Thickens:</strong></p>

<p>Okay, the User Control detail turned out to be a bit of a red herring. The Details.aspx page accepts a query string parameter indicating which item to show. If that parameter is blank, it redirects back to Default.aspx. It appears what's happening is Default.aspx receives the cross-page postback, and when it checks PreviousPage, the Page_Load() method of Details.aspx is run.</p>

<p>However, it looks like the query string parameter isn't sent back to Details.aspx, so it hits this error checking code and runs the Response.Redirect() error handler.</p>

<p>It appears what I need to do is have Details.aspx check whether it's being executed as PreviousPage in a cross-page post-back, and skip most of the execution that it would normally do. How can I check for that condition? Page.IsPostBack doesn't seem to be catching it.</p>
