user1577242
user1577242

Reputation: 413

Calling a c# methode from javascript/jquery and get the result

I have a dialog in an ASP.Net,c# application.This dialog has a textbox.When I choose save I want to call a function from C# who makes some verifications in the database and then to get the result in javascript/jquery.If the inserted value is true I want to close the dialog other way to remain opened,but I can't succed to close the dialog box after i receive true from c# function.Below is the code:

ascx :

    <div id="popup" title="Choose Delegate">
        <label>Delegate<label><input type="textbox" value="" name="inputD" id=="inputD"/>
     </div>

Javascript:

$('#btnAdd').click(function(e){
$('#divPopup').slow("show");
$('#divPopup').dialog({
      height:150,
      width:300,
      modal:true,
      buttons:{
               "close":function(){$(this).dialog("close");}
               "save":function(){
                  var obj=document.getElementid("inputD");

                  $.ajax({
                    type: "POST",
                    url: "add.aspx/check",
                    data: "{delegate: '" + obj.Value+"'}",
                    contentType: "application/json; charset=utf-8", 
                    dataType: "json",
                    success: function (msg) {
                          rez= "OK";
                          $(this).dialog("close");
                         },
                    failure: function () {alert("FAIL"); }});                    }
        });
      }

C#:

[WebMethode]
public static Boolean check(string delegate)
{
   .....

   return true;

  }

C# methode returns corect value.

I try also this :

       $('#btnAdd').click(function(e){

           $('#divPopup').slow("show");
           $('#divPopup').dialog({
                       height:150,
                       width:300,
                       modal:true,
                       buttons:{
             "close":function(){$(this).dialog("close");}

            "save":function(){
                  var obj=document.getElementid("inputD");
                  var rez ;

                  $.ajax({
                    type: "POST",
                    url: "add.aspx/check",
                    data: "{delegate: '" + obj.Value+"'}",
                    contentType: "application/json; charset=utf-8", 
                    dataType: "json",
                    success: function (msg) {
                          rez= "OK";
                                                       },
                    failure: function () {alert("FAIL"); }
                 });       

                    if (rez="OK")
                          $(this).dialog("close");

         }
        });

But it doesn't see the rez value in this case.

Thanks !

Upvotes: 0

Views: 250

Answers (2)

Hassan
Hassan

Reputation: 1433

You can use an Ajax Call in your "save":function(e) and just check the returned value if true close dialog, else remain opened

Ajax calls are really simple to implement, I let you search that :)

Upvotes: 1

VDP
VDP

Reputation: 6420

You need a web-service on the server side. (preferably REST)

http://restsharp.org/ is an easy to use library for that.

Take a look at this question for more info.

In the front end you make an ajax call to you're REST api (I see you use jquery so it won't be that hard ;))

Upvotes: 0

Related Questions