0

OK so I have a jquery modal dialog. It accepts an input and then has an ok and and cancel button. Cancel works fine by simply closing the dialog. The ok button fires the OnClientClick when I really want it to fire the OnClick method so I can go into the server controls and log the input into the database using ASP.NET. Any ideas on how this is supposed to be done using jquery?

Side note: I'm not currently by the computer that has the code, but I'll try to update it as soon as I can.

auwall12688
  • 389
  • 3
  • 11
  • 23
  • Can you solve this via a `PageMethod`? In that case you can always use jQuery's `$.post()` http://api.jquery.com/jQuery.post/ – NoLifeKing Jul 03 '12 at 12:20

2 Answers2

0

In your codebehind create a static function and add the [WebMethod] attribute, so now you can fire code behind methods from Jquery.

javascript:__doPostBack('IDOfYourControlButton','')

This is the same method a button or linkbutton uses to fire the onClick event in .NET

Demo webmethod in code behind you add this

[System.Web.Services.WebMethod]
public static string SayHi()
{
    return "Hi";
}

in yr aspx file you add this javascript

<script>
        function GetHi() {

            PageMethods.SayHi(onComplete);
        }

        function onComplete(result) {
            alert(result);
        }

        GetHi();
</script>

and dont forget to add this also in yr site.master or aspx page right under the

    <form runat="server">
    <asp:ScriptManager ID="scriptManager" EnablePageMethods="true" runat="server"/>
</form>
JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
  • Could you elaborate a little bit on how to call the method from jquery? – auwall12688 Jul 03 '12 at 12:59
  • If you would call __doPostBack with javascript it will simulate the button on click event. for that u dont need JQuery. but if you want to call a normal method/function in code behind then you would need the web methods to fire those methods. i will try to post u a sample using web methods – JohnnBlade Jul 03 '12 at 13:02
0

You could try this:

Insert a ASP.NET button on your form, binded to the event that you want to trigger, then, with jQuery do $('#your_button_ID').click(); on the 'OK' event of your dialog.

Hope it helps!

marcoaoteixeira
  • 505
  • 2
  • 14