Kevin
Kevin

Reputation: 1240

Postback Causing Page to Load as if for the First Time

I am using:

Response.Redirect(Request.RawUrl);

to force a postback on my web page. However, the path of execution in the code is as if the page were loading for the first time. How do I cause just an ordinary postback?

Any advice is appreciated.

Regards.

EDIT:

This post seems to be attracting a lot of negativity. I got this method from a Stock Overflow post after a Google search for forcing a postback. So the downvoters should probably take out their frustrations on that post instead of mine.

To clear up some of the misapprehensions about my question:

I have an event Button_Click that contains logic in the method body -- obviously. However the flow of execution of the code goes like this:

Button_Click --> Page_Load (IsPostBack == true) --> Button_Click body --> execution ends.

So my problem is that none of the logic in the body of Button_Click is showing up on the page because the postback is done before any of the code in the body of Button_Click is executed.

EDIT:

Here is the event:

protected void RedoEdits_Click(Object sender, EventArgs e)
{
    // Goes to Page_Load here with IsPostBack == true
    string trendsFileLocation = currDir + "\\" + reportDir + "\\" + trendsFile;
    string messagesFileLocation = currDir + "\\" + reportDir + "\\" + messagesFile;
    if (File.Exists(trendsFileLocation))
    {
        File.Delete(trendsFileLocation);
    }
    if (File.Exists(messagesFileLocation))
    {
        File.Delete(messagesFileLocation);
    }
    trendsXML.Clear();
    editMode = "redo";
}

Code snippet for IsPostBack == true:

if (editMode.Equals("redo"))
{
    editMode = "";
    ViewReport_Click(null, null);
}

The code snippet never gets exercised because the postback happens first, skips the snippet because editMode != redo, then finishes with the body of RedoEdits_Click which finally sets editMode == redo but since the postback has already happened we don't get to ViewReport_Click.

And btw, editMode is a session variable so it persists.

Upvotes: 0

Views: 9291

Answers (4)

Kevin
Kevin

Reputation: 1240

I needed the page reload to restore my original, unedited, HTML. The process of editing the table replaced some InnerHTML and I needed to start over to redo the edits. So I just moved the logic necessary for redoing the edits in the code for IsPostBack != true and stayed with Response.Redirect(Request.RawUrl). Works like a charm.

Upvotes: 0

walther
walther

Reputation: 13600

You can't force a postback from your code-behind. How would you do that? Postback occurs when you click a button for example. Even if it was possible to force it from code-behind (surely there's a way), what would you achieve by that? Just call a method you want, don't do stupid things like this.

Postback = posting data/request back to the same page. Redirecting obviously won't work.

EDIT based on OPs edits:

You need to rethink your workflow, so maybe do it like this:

protected void RedoEdits_Click(Object sender, EventArgs e)
{
    // Goes to Page_Load here with IsPostBack == true
    string trendsFileLocation = currDir + "\\" + reportDir + "\\" + trendsFile;
    string messagesFileLocation = currDir + "\\" + reportDir + "\\" + messagesFile;
    if (File.Exists(trendsFileLocation))
    {
        File.Delete(trendsFileLocation);
    }
    if (File.Exists(messagesFileLocation))
    {
        File.Delete(messagesFileLocation);
    }
    trendsXML.Clear();

    ViewReport_Click(null, null);
}

It really isn't a good idea to try to emulate the postback.

Or, maybe even better approach - define a new Event, let's say EditModeActive (or whatever name you want), that will fire up every time you assign editMode = "redo". Then just subscribe to this event and do whatever you wish.

Upvotes: 1

David
David

Reputation: 73554

That's because redirecting DOES load the page as if for the first time. It's not posting back at all. This may be in an event handler like a button click, but once you redirect in that postback event handler, it is no longer in the postback of the same instance, you're reloading the page from scratch.

Example

private void MyButton_Click(Object sender, EventArgs e)
{
   lblStatus.Text = "Button Clicked!";  // still within a postback
   Response.Redirect(Request.RawUrl);  // now leaving the postback.  At this point, exexution stops and the page is loaded from scratch, as if we're not in a postback.

   lblStatus.Text = "Done posting back";  // This code will never execute.,  Response.Redirect throws a System.ThreadAbortException, and by default, code execution stops once Response.Redirect happens.

}

If you want to trigger a true postback without then loading the page from scratch, put a button or some control on the screen and use that to trigger a real postback, and don't call Response.Redirect().


Added because of hte comment below:

Familiarize yourself with the ASP.NET Page Lifecycle. If you hace things happening in Page_Load that you don't want to occur within the postback, then you should surround them with

if(!Page.IsPostback)
{
  // stuff that should only happen on initial load here.
}

OR consider doing those things in a different event. For example, if you're creating controls on teh fly, it should be done in Page_Init because it happens BEFORE the ViewState is applied. If done this way, the controls are created in Page_Init on each postback, and THEN the values are applied from teh Viewstate, and THEN you reach Page_Load, where you can work with them.

Upvotes: 2

Peter Kiss
Peter Kiss

Reputation: 9319

Response.Redirect() isn't cause any postback becouse it only sends a simple GET request to the server.

Upvotes: 0

Related Questions