17

Is there a way to force a postback in code?

I'm looking to force the raising of a postback from a method in the c# code behind my asp.net web application.

shaun.breach
  • 229
  • 1
  • 3
  • 11
  • 1
    Can you be more specific - and tell us what approaches you have tried so far? – Richard Ev Nov 21 '11 at 09:56
  • 1
    eh, a postback is from client (browser) to server. So C# code (that runs on the server) can't do that. – Hans Kesting Nov 21 '11 at 09:57
  • I think your question is similar to the one raised [here](http://stackoverflow.com/questions/1418532/how-to-force-a-postback-with-asp-net-and-c-sharp) – seanzi Nov 21 '11 at 09:59
  • @Richard Ev I'm looking to dynamically add UserControls to the web app. Clicking a button will increment the number of controls, but the new control will not be displayed until the `Page_Load` method fires on postback (after the next button click). So I need to force a postback at the end of the method that increments the number of controls. – shaun.breach Nov 21 '11 at 10:09
  • possible duplicate of [How do I force full post-back from a button within an UpdatePanel?](http://stackoverflow.com/questions/2545508/how-do-i-force-full-post-back-from-a-button-within-an-updatepanel) – TylerH Apr 22 '15 at 14:44

8 Answers8

10

You can try redirecting to same page.

Response.Redirect(Request.RawUrl);
sth
  • 222,467
  • 53
  • 283
  • 367
live-love
  • 48,840
  • 22
  • 240
  • 204
9

A postback is triggered after a form submission, so it's related to a client action... take a look here for an explanation: ASP.NET - Is it possible to trigger a postback from server code?

and here for a solution: http://forums.asp.net/t/928411.aspx/1

Community
  • 1
  • 1
mamoo
  • 8,156
  • 2
  • 28
  • 38
  • 2
    For those using newer versions of .NET, you have to use `Page.ClientScript.GetPostBackEventReference` since 'this.GetPostBackEventReference(...)' is obsolete. Also probably `Page.ClientScript.RegisterStartupScript(...)`. – AzNjoE Feb 29 '16 at 21:33
6

Simpler:

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "DoPostBack", "__doPostBack(sender, e)", true);
devilcius
  • 1,764
  • 14
  • 18
3

Here the solution from http://forums.asp.net/t/928411.aspx/1 as mentioned by mamoo - just in case the website goes offline. Worked well for me.

StringBuilder sbScript = new StringBuilder();

sbScript.Append("<script language='JavaScript' type='text/javascript'>\n");
sbScript.Append("<!--\n");
sbScript.Append(this.GetPostBackEventReference(this, "PBArg") + ";\n");
sbScript.Append("// -->\n");
sbScript.Append("</script>\n");

this.RegisterStartupScript("AutoPostBackScript", sbScript.ToString());
Vortex852456
  • 721
  • 6
  • 23
1

By using Server.Transfer("YourCurrentPage.aspx"); we can easily acheive this and it is better than Response.Redirect(); coz Server.Transfer() will save you the round trip.

Ensari
  • 11
  • 1
  • Doesn't this reset the form fields? I guess it's like a fresh page call which means user-inputs like filters would be undone (if they aren't stored in session). – Vortex852456 Oct 14 '15 at 10:16
1

No, not from code behind. A postback is a request initiated from a page on the client back to itself on the server using the Http POST method. On the server side you can request a redirect but the will be Http GET request.

Andy Rose
  • 16,770
  • 7
  • 43
  • 49
1

You can use a data-bound control like the Repeater or ListView, re-bind it to a list of control properties as needed, and let it generate the controls dynamically.

As an alternative, you can use Response.Redirect(".") to re-load the same page.

Mihai
  • 2,835
  • 2
  • 28
  • 36
0

You can manually call the method invoked by PostBack from the Page_Load event:

public void Page_Load(object sender, EventArgs e)
{
    MyPostBackMethod(sender, e);
}

But if you mean if you can have the Page.IsPostBack property set to true without real post back, then the answer is no.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208