Itay.B
Itay.B

Reputation: 4111

jQuery-UI Dialog

I'm using this code sample that came from jquery-ui website to show the dialog form. How can I add functionality to the "OK" button. I want this button to invoke a javascript function "PageValidation", and if it returned true, then call a C# function that will save the data in the db, and then close the modal form. If "PageValidation returned false then do nothing and don't close the modal form.

This is the code:

            $('#dialog').dialog({
                autoOpen: false,
                width: 600,
                buttons: {
                    "Ok": function() { 
                        $(this).dialog("close"); 
                    }, 
                    "Cancel": function() { 
                        $(this).dialog("close"); 
                    } 
                }
            });

Thanks.

Upvotes: 0

Views: 148

Answers (3)

Saeed
Saeed

Reputation: 7370

You can add your function in the "OK" function like this:

                "Ok": function() { 
                    MyFunc();
                    $(this).dialog("close"); 
                }, 

then, write the body of MyFunc() , like this:

function MyFunc(){
     if (PageValidation())
           AjaxCSharpCall();
}

and finally write the body of AjaxCSharpCall() to send an ajax request to a page, in which you can do your C# function. something like this:

function AjaxCSharpCall()    {
     $.ajax({
       type: "POST",
       url: "MyPage.aspx",
       data: "doFunction=True",
       success: function(){
         alert( "C# Function was executed!" );
       }
     });
}

Upvotes: 1

Fender
Fender

Reputation: 3055

just make a button (hyperlink) with that functionality and copy the javascript command from the button to the "Ok" event from your dialog. now you can delete your button and use it with your dialog

you javascript should look like that:

__doPostBack('ctl00$ctl00$OverallMainContent$MainContent$Your_Control','yourFuncOnServer')

just try adding a button as a hyperlink and copy it

Upvotes: 1

Sandeep Bhat
Sandeep Bhat

Reputation: 165

Write your OK something like this:

"OK": function() {
    saveData();
}

Then write saveData() something like this:

function saveData() {
   if (pageValidation()) {
     /* C# function here. */
     $("#dialog").dialog('close');
   }
}

Hope that helps :)

Upvotes: 0

Related Questions