views:

1645

answers:

8

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

+4  A: 

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.

A: 

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

kenny
A: 

Thanks Kenny, I know this trick already, and it is very cool, actually. but my problem to day is way more then syntax or intelisense. the forms plugin was not meant to work with .net, and we need to tweak it a little. And that's what I'm looking for. a asp.net form using jquery form plugin. Thanks, Dave

+1  A: 

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

Glenn Slaven
+1  A: 

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:

vitorsilva
+2  A: 

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

Corey Downie
A: 

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.

goodwill
A: 

why not just post the data to a separate form page? That is what we are in the middle of doing using Jquery and the form plugin. A the time of this post we are getting a viewstate server error. Any suggestions?

Tom