views:

893

answers:

4

I currently have a simple form that when you click the "save" button will persist it to the database.

I would like to use JQuery to do the callback for me and popup a "Save completed" div window/div of some sort.

My only problem is how do I call the btnSave_Click() event from JQuery?

If I use PageMethods the method would have to be static and therefore lose access to my textboxes and other page controls?

Thanks, Goosey

A: 

You can use onClientClick

waqasahmed
And this will call my JS function, which in turn will call my PageMethod, which is declared static and has no access to the page controls, I can get all the form data via JQuery, but would rather do it via code-behind.
Goosey
A: 

Have a look at the jQuery Form Plugin, it can change existing forms into Ajax forms.

Kobi
Thanks for the link. I am actually just adding AJAX to an existing asp.net form. All of my logic and methods in code-behind already exist and work just as I want them to.I just want to be able to do it with a callback (updatepanels) instead of a full postback, then how a nice jquery popup window that says "Saved" or something.I guess I could call my JS from my code-behind instead of the other way around?
Goosey
A: 

You need to set __EVENTTARGET to the id of the control that you want to simulate causing the postback if you want to use the same handler. I seem to recall having to replace the underscores with dollar signs as well, but I could be wrong on that. The hidden inputs, __EVENTTARGET and __EVENTARGUMENT, are used by the framework to identify which control caused the postback. There's a nice discussion of the server side issues in this blog post, though it doesn't talk about AJAX. Google for __EVENTTARGET and postback for more info.

tvanfosson
Thanks for the reply and while this is technically correct it wasn't exactly what I was looking for, I asked the wrong question but you still gave a correct answer.
Goosey
+1  A: 

Are you explicitly trying to avoid passing the values of the input controls? because that would be much easier.

Using a lightweight jQuery call to do the post but then expecting a full control hierarchy in the code behind to pull data out? What's the intent here? If you require that, it would probably be easier just to submit the page, and register javascript to run to pop the success message up on load.

Personally, I think the page method route and $.ajax or $.post is a much cleaner, separate way to solve the issue. That way you can just show the popup as part of the success callback.

TreeUK
My intent is to use callbacks and partial rendering instead of full post-backs. The site is already in place and all code/methods that handle what to do with submitted form data works as it should. While it is a lot cleaner to do it the correct way. I really would hate to re-do alot of that code which would include a new set of regression tests of the modified code.Really I just want to pass less data using callbacks and not reload the entire page every time.So I may just go the register to run on startup way... but how would I do this with UpdatePanels?Thx
Goosey
I got it worked as I wanted by using the method you mentioned... and just for anyone else trying this make sure to use ScriptManager.RegisterStartupScript() and not ClientScript.RegisterStartupScript() when in an UpdatePanel.
Goosey