2

I need to cause a postback in c#, how can i do this? (It can not be through a button or any other element) Just want to cause a postback if a condition is met.

something like

  If(so and so)
      Postback now!
  else
      Do not post back
user710502
  • 11,181
  • 29
  • 106
  • 161

8 Answers8

5

Your question does not make sense.

C# code runs on the server, in response to a postback.
Until a postback happens, no code can run.

You may want to trigger a postback in Javascript, which runs in the browser.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • So I can not trigger a second postback from the code behind?, My question may not make sense because I am new to C#, but i was hoping it could be done... – user710502 Jul 05 '11 at 21:53
  • You can, but you probably shouldn't. Why? What are you trying to do? – SLaks Jul 05 '11 at 21:54
  • Why the secrecy about it?,,, I just need to call a postback to see if I can restore a CuteEditor that is collapsing but I noticed that when I postback clicking on a button or something it restores... (this is happening in FireFox Only) – user710502 Jul 05 '11 at 21:55
  • I don't know what the problem is, but there is probably a better way to solve it. You can do it using `RegisterClientScript`. – SLaks Jul 05 '11 at 21:58
5

From the comments it looks like you are using telerik's RadTabs. You could potentially set AutoPostBack to true on the tab control so that it would force a refresh whenever the user switches tabs.

http://www.telerik.com/help/aspnet/tabstrip/tab_server-side%20events.html

Kelly Robins
  • 7,168
  • 6
  • 43
  • 66
  • @user710502 Tip for the future, describe *why* you need to do what you need to do as well, it often helps when people are trying to understand what you're trying to do. Also, consider not asking about what you think is the solution, but instead ask about the underlying problem. – Lasse V. Karlsen Jul 05 '11 at 22:25
  • `this.Load += new System.EventHandler(this.Page_Load);` This line did what I needed from the above link thanks Apoca. – Anicho Aug 31 '11 at 14:11
3

To force a postback in Server code, use ClientScript.GetPostBackEventReference() to generate the javascript code that does the postback. Then add a startup script in your event handler that calls the postback javascript.

aspx:

// Do an empty postback that doesn't do anything.
function doPostback() {
  <%= ClientScript.GetPostBackEventReference(btnDoPostback, String.Empty) %>;
}

<asp:Button ID="btnDoPostback" runat="server" OnClick="btnDoPostback_Click" Style="display: none;" />

cs:

if (!IsPostBack) {
  ScriptManager.RegisterStartupScript(this, this.GetType(), "dopostback", "doPostback();", true);
}

protected void btnDoPostback_Click(object sender, EventArgs e)
{
}
Rob de la Cruz
  • 1,182
  • 1
  • 8
  • 4
3

Some didactic contextualization: whatever piece of code you are trying to call in your "second postback", just call it in your "first postback" already!

Example:

You have a method you want to call, say, a Button_Click in your "second postback"? Just call it in you "first postback":

btnSaveClick(btnSave, null);
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
3

You can always do a Response.Redirect(Request.RawUrl);.

You'll want to be careful that you don't cause an endless redirect loop.

Aaron Barker
  • 706
  • 7
  • 14
1

You can't do a postback in your code-behind. Your code-behind code IS your postback code. Why are you trying to do this? Maybe we can help you in your program logic

Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
  • I was hoping I could make a call to the server from the code behind at an arbitrary time.. i am having issues with a CuteEditor that is collapsing when returning from another RadTab control... so i have 3 tabs and the first has the Editor, when I go from tab 2 to tab 1 it collapses.,. but if i postback it restores – user710502 Jul 05 '11 at 21:58
1

Did you want to call the page again? You can do this with a Response.Redirect

Leah
  • 3,322
  • 2
  • 31
  • 33
0

Do you mean on your .aspx/cshtml pages? If so use jquery's $.post

If you really are in a controller (and you are already running in response to a postback) then you should probably refactor your code so that whatever logic you want to call (via your 'second post' back) can be called from your existing location as well as your other Post action.

NinjaNye
  • 7,046
  • 1
  • 32
  • 46