How to force a postback with asp.net and C#

2020-07-11 06:18发布

问题:

I have a demo scheduled with a client and i need a quick and dirty fix for now. I will find a more appropriate work around tomorrow but for the time being i need a way to force a post back, or refresh the page.

i tried:

Response.Redirect("");

but it brings me to a page that says "Object moved to here". 'here' is a hyperlink that brings me to the page with desired results but i wish to bypass this message.

Any ideas.

回答1:

Response.Redirect("default.aspx");

(or whatever the name of the current page is)



回答2:

Response.Redirect(Request.RawURL); 

This also works and you won't need to worry about putting in the path.



回答3:

Response.Redirect() is not the greatest because there is not state. It's a new request. if you want to keep state of all your controls then use the __doPostBack method which is added automatically by ASP when the page is rendered so it's accessible from client side:

you can do this:

or just call it from javascript:

__doPostBack('myElementId','');

Alternatively you can just use javascript code:

document.forms[0].submit();



回答4:

Couldn't you just add a javascript block with window.reload() in it?

Here is some useful info on how to do this correctly in Web Forms.



回答5:

The server can not tell the client to reload.

You can use the html meta refresh:

 <meta http-equiv="refresh" content="2;url=http://the.new.url">

but that will not do a proper post back i think.

Content is how many seconds the client waits to do the refresh.



回答6:

Take a look at ASP.NET AJAX Timer Control! http://www.asp.net/ajax/documentation/live/tutorials/IntroToTimerControl.aspx



回答7:

Do you need a post back to populate a list? Did you look into if solving it with Ajax could help??

Or if you just need a quick and dirty thing, just fake it and fix it later.