7

in asp.net 2.0 (not mvc), the form's action is to itself. how can I use the forms plugin to send information to server? I need to send data from the form (let's say name, email, comment) and to display the result on the client side. Any ideas?

Thanks, Dave

0

7 Answers 7

4

ms ajax? if i use update panel, i don't need jquery. i want to use jquery and the form plugin (plus the validation plugin) only. no microsoft ajax for me, thank you! just look at the trafic they produce in firebug to understand why.

3

It depends on how much of asp.net you want to use during the form submit. I am using the forms plugin in this same way but you need to think in terms of a more classic web model.

The forms plugin does a 'submit' which does not include any viewstate information. That is to say, if you try to get a value like so

sName = txtName.text

the text for txtName will be blank. But if you use your request object you should be able to pull back the value provided your know the control's UniqueID

sName = Request.Form(txtName.UniqueID)

Then what I would do is use the form plugin's success: callback to run an ajax call that will pull back your results. You can use ms ajax WebMethods for this, and you can call the webmethods directly from jquery without the need for the ms script manager. In this case, the WebMethod is returning the html I want displayed on the page.

$(form).ajaxSubmit(function(){
   success:function(ret){
     $.ajax({
       type: "POST",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       data: "{}",
       url: "SomePage.aspx/SomeWebMethod",
       success: function(msg){
         $('#somediv').html(msg); 
       }
   }
});

More info on calling ms ajax with jquery here

1

Because of the way viewstate is tightly coupled to the form in ASP.NET, unless you set up a service to accept the request I'm not sure if this is going to be possible. If you don't want to use viewstate, then you could just use a normal HTML form on the aspx page and submit that to a service that returns your expected results.

When you submit an asp.net form to postback, it sends everything back through the page lifecycle, I don't think there really is anything for a jQuery ajax request to talk to. You could setup a WebMethod on the page, which essentially exposes a service and you could get the jQuery request to talk to that, but I'm not conviced that it would work.

However, you can do ASP.NET AJAX with the MS libraries without using UpdatePanels, see this article for a good rundown of what you can do with WebMethods and the ajax javascript libraries

1

I ran into this problem today and eventually solved it using the following combination of hacks:

Suppose you have a search page and a results page and you have a form on the search page that you want to post to the results page and load via ajax using the jquery forms plugin. You need to do the following:

  1. Create a Search.aspx page and a Results.aspx page

  2. ASP.NET webform pages have a single form on a page with and id of aspnetForm but the action will always be set to post to itself, so the first thing you need to do when you load the Search.aspx page is to change the action of the aspnetForm to be Results.aspx like this:

    $('#aspnetForm').attr('action', 'Results.aspx');

  3. You then need to deal with viewstate so that when you POST from Search.aspx to Results.aspx you don't get invalid viewstate errors. To do this just remove the viewstate variable from the page like this:

    $('#__VIEWSTATE').remove();

  4. then you should be able to set up the #aspnetForm to use the jquery forms plugin like so:

    $('#aspnetForm').ajaxForm();

This then allows you to post to the Results.aspx webform. This is just a start to get you going in the right direction, but basically it comes down to needing to change the action that the aspnet form posts to and then removing viewstate.

1

I currently use a mix of asp.net and jquery and the way i solved the issues with the page life cycle and such is to simply not use the autopostbacks and asp.net buttons to submit the form.

I use either ajax calls attached to simple html buttons or, when i really want to submit the entire page i use the __doPostBack(eventTarget, eventArgument) javascript function.

These articles were useful to me:

0

It doesn't seem VS08 works for you, but others may be interested: I went to a MSDN Roadshow the other day that seemed to make it very easy. Scott Gu's blog has this: http://weblogs.asp.net/scottgu/archive/2008/11/21/jquery-intellisense-in-vs-2008.aspx

0

It doesn't work, as ASP.NET force you to put everything within a server form as long as you use server control (this is the way how it handles postback). The primary problem you would have is HTML does not allow nested form anyway, so you can't even use jQuery to find the form element (thats my experience with FF3)

The good answer (but hard to achieve)- throw away WebForm and use MVC.

The compromise workaround- I did a small plugin myself which converts a div into an ajax submit with the div abused with method="post" and action="url" dress up, then utilize jQuery Form Plugin to serialize, submit with a plugin pattern. It doesn't do File Upload though (as that requires IFrame, I think its still feasible, but take some more hack). The code is in my client's project so I still dont have permission to post it as a plugin. However I think this is not too hard to do it once you know the theory :)

Really, aim for the good answer (get rid of WebForm) next time. Its not just jQuery Form going to hurt you, there's a lot more pain you have to take if you decided to do jQuery + Web Form, if its not my client's requirement I would never take this path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.