1

Possible Duplicate:
ASP.NET postback with JavaScript

I have a page without a form. But I need that everytime that this page is requested, the postback is done by the javascript.

How can I do the PostBack via Javascript ?

Community
  • 1
  • 1
Lucas_Santos
  • 4,638
  • 17
  • 71
  • 118
  • What's wrong in using form? Use AJAX UpdatePanel so that your page doesn't get refreshed. – Moon Oct 28 '11 at 14:10
  • "Everytime the page is requested, the postback is done by Javascript" I'm not sure what is meant by this. Everytime someone clicks a hyperlink or types in the page's URL, you want Javascript to handle loading the page? If there is no form on the page, then why would the page "postback" to anything? Sorry, just a little confused. – Robert Iver Oct 28 '11 at 14:10
  • In this page, I just show some informations that come from my database. But is this, when click in the hyperlink, i need a postback. – Lucas_Santos Oct 28 '11 at 14:15

2 Answers2

3

You can use the jQuery.post() method.

http://api.jquery.com/jQuery.post/

Example:

$.post("test.aspx", { name: "John", time: "2pm" } );
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
  • Great! Now the Page_Load is called everytime that I do the request! Thank you! – Lucas_Santos Oct 28 '11 at 14:22
  • Interesting. Will this be treated as an actual postback? In other words, when this .post() operation is executed, if I have a breakpoint in my code-behind, will IsPostBack == true? – mikemanne Oct 28 '11 at 14:27
  • @mikemanne: Yes, this will do an HTTP POST. – D'Arcy Rittich Oct 28 '11 at 14:33
  • @RedFilter: just did a quick test. While that does indeed send an HttpRequest of Mode POST, asp.net does not seem to recognize it as a postback: the page's IsPostBack property returns false. Is there something I'm missing? – mikemanne Oct 28 '11 at 14:55
  • I realize the test and I got IsPostBack property return false also. – Lucas_Santos Oct 28 '11 at 15:05
  • Which page are you talking about, the initial page load, or the AJAXed page? – D'Arcy Rittich Oct 28 '11 at 15:06
  • @RedFilter: both the initial page load, and AJAXed page request result in IsPostBack being false. My understanding is that IsPostBack is more than just a shortcut for "Request.Method =='POST'"; it specifically refers to a form being posted-back. That's why I was surprised that an Ajax post operation would be considered a "postback". – mikemanne Oct 28 '11 at 15:15
  • I thought the question was regarding the HTTP protocol, not the .NET page event. My earlier statement must be incorrect, on reflection I would not be surprised if the PostBack event does not fire... – D'Arcy Rittich Oct 28 '11 at 15:22
0

My understanding is you cannot do a postback without a form tag - period. A form is an essential part of how the ViewState works.

You can use javascript to drive the browser to a different URL, or reload the current URL, if that's what you're looking for. However, that's not an ASP.NET postback.

mikemanne
  • 3,535
  • 1
  • 22
  • 30
  • I need this because, my page just enter in my Page_Load method in the first request. If I request again, the page don't enter in my Page_load, and then I'll show to my user old values. So, I need to do a postback – Lucas_Santos Oct 28 '11 at 14:12
  • Please don't downvote me because ASP.NET Postbacks require a form tag. That's not my fault. If my understanding of that requirement is wrong, please explain - I'm happy to learn. – mikemanne Oct 28 '11 at 14:14
  • Page_Load will be called on every page request, whether it is an "original" request or a postback. – mikemanne Oct 28 '11 at 14:14
  • Which are the reasons that my Page_Load is called just in the first request in my page? – Lucas_Santos Oct 28 '11 at 14:18
  • Page_Load is called on every request for that page. If you have an if(IsPostBack) block in your Page_Load, the code in that block will ONLY be called in case of a postback. But unless there's an HttpModule interfering, or your page throws an exception earlier in the lifecycle (http://msdn.microsoft.com/en-us/library/ms178472.aspx), Page_Load will be called. – mikemanne Oct 28 '11 at 14:21