开发者

Using jQuery with ASP.NET to Submit a Form

I've used jQuery dozens of times with PHP with great success. I'm working on an ASP.NET application and would like to use jQuery in the same manner.

Basically, I've got a maste开发者_运维问答rpage that has the form and a webform that has all the form fields and data. A user can submit the form multiple ways - selection of a drop-down, button, etc. I want to catch all submits and use jQuery to submit the form. While the form is being processed, I want to display a new DIV with some text in it. Finally, I want to replace that div with the new form.

How can I accomplish this with the way that ASP.NET works?


Actually ASP.NET will post-back if you use its built-in JavaScript __doPostBack function. There's no other painless way for doing that.

That means you can use jQuery to handle drop-down lists, buttons or whatever (X)HTML element event and handler's body will invoke __doPostBack.

It's unclear that you want is a full-postback, but a partial one using AJAX.

If you're looking for a solution for sending form values to the server without a full-postback, I believe you've these options:

  • Callback API: http://msdn.microsoft.com/en-us/library/ms178208.aspx
  • Page methods, update panels: http://msdn.microsoft.com/en-us/magazine/cc163480.aspx

Anyway, let me give you an advise: ASP.NET works quite different compared to PHP and you'd not try to reproduce some known PHP solutions in ASP.NET. You need to change your mind.

About showing a DIV or anything while something is processed, play with initializeRequest ASP.NET AJAX PageRequestManager:

  • http://msdn.microsoft.com/en-us/library/bb397460.aspx

But that would depend on what AJAX API you're using, because since Microsoft AJAX will be replaced by jQuery in the next times, I'll need to say that you need to do that in some jQuery approach, like creating some $.ajax wrapper so your code will be able to listen when an asynchronous request is going to be made and you can perform actions by handling that situation like showing a DIV or any loading notice.


In ASP.NET Webforms formposts aren't as easy as they are in php. If you're new in ASP.NET development try http://www.asp.net/mvc. A common framework which allows you to implement TypedViews (ViewModes), simple request to modelbinding, and so on...

mh, sample:

[HttpPost]
public JsonResult Insert(string name, string vorname) // name&vorname filled by $_POST:)
{
    var @new = new Person { Name = name, Vorname = vorname }
    this.repo.Insert(@new);

    return this.Json(new { success = true, newId = @new.Id });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜