views:

2856

answers:

3

Is it possible to to programmatically trigger a postback from server code in ASP.NET? I know that it is possible to do a Response.Redirect or Server.Transfer to redirect to a page, but is there a way to trigger a postback to the same page in server code (i.e. without using javascript trickery to submit a form)?

A: 

You could do it with an Ajax request. You'd have to have the page polling the server. There isn't any way for the server to push information out to the browser without requesting it. But having some Javascript that runs in the background and polls the server every 5 seconds (or more, depending on your needs) would probably be the best solution.

APPEND

If you go this route, you can have the server send back just a yes or no, or even just 0 or 1 depending on whether or not the postback should be performed. Depending on your needs, there many be no reason to actually use the XML part of AJAX. Just run a simple Asynchronous request, possibly with a few querystring variables, and get back a simple one word, or even a number as a response. That way you can keep the javascript for creating and parsing the XML out if it isn't needed.

Kibbee
Did you even read his question?
FlySwat
+5  A: 

Asp.net Postbacks are initiated from the client (typically form submission). I am not sure what you are trying to achieve. Some of the server side page lifecyle events are already executed and what you are trying to do is raise the previous event handlers again.

Gulzar
+1  A: 

Postbacks are caused by a FORM submission. You need to initiate them from the client.

FlySwat