Question

I have a button, that open a dilag Jquery and show some div. The dialog show 2 buttons ("ok "and "Cancel").

I would like to execute a code behind function (C#.net) when I click in the dialog "OK".

How can I do that?

$("#dialog").dialog({
    autoOpen: false,
    modal: true,
    title: "Image Full Size",
    draggable: true,
    resizable: false,
    show: 100,
    width: 900,
    height: 680,
    buttons:
        {
            "Ok": function () {
                $(this).dialog("close");
            },
            "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });

For example:

To call next below function in C#.net

public void loadInfo()
{
    // Do something
}
Was it helpful?

Solution 2

    one way that we needed to add our method as a webmethod, if we want to call   a method from code behind at client side   Write your loadInfo() Method server side    in   the  CS file and calling it from client side using JQuery. 

see below code

  $("#dialog").dialog({
       autoOpen: false,
       modal: true,
       title: "Image Full Size",
       draggable: true,
       resizable: false,
       show: 100,
       width: 900,
       height: 680,
       buttons:
      {
           "Ok": function () {
             $.ajax({ type: "GET",
             contenttype: "application/json; charset=utf-8",
            data: "{null}",
           url: "WebService1.asmx/loadInfo",
           dataType:"json",
            success: function(res) {
           // Do something means binding data
     },
     error: function(err) {
     alert(err);
 }
   });   
         $(this).dialog("close");
        },
        "Cancel": function () {
            $(this).dialog("close");
        }
      }
 });

OTHER TIPS

A really quick and simple way to get this functionality is to use a hidden field with the event OnValueChange.

<asp:HiddenField ID="hf" clientidmode="static" runat="server" OnValueChanged="hf_ValueChanged" />

Then in your script:

"Ok": function () {
      $(this).dialog("close");
      $("#hf").value("1");
},
"Cancel": function () {
      $(this).dialog("close");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top