如何在同一个onclick事件上调用客户端和服务器端功能?

问题描述:

大家好,

我想同时调用两个功能,即客户端和服务器端。这是我的代码,我想从客户端调用showPics()并传递字符串JobStatusId。



JQery: -



Hi all,
I want to call two function, Client Side And Server Side at same. Here is my code where I want to call showPics() from client side and pass string JobStatusId.

JQery:-

    function call(i) {

        $.ajax({

               type: "POST",

               url: "Jobs.aspx/showPics",

               data: JSON.stringify({ JobStatusId: i }),
               
              contentType: "application/json; charset=utf-8",

              dataType: "json",
              
               success: function () {
               
                   alert(i);

               },

               error: function () {

 

                   alert("failed");

               }

                      });
}



这是CS代码: -




And It is CS code:-

protected void gvJobStatus_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var lnkMap = (LinkButton)e.Row.FindControl("lnkOnMap");
            lnkMap.Attributes["onclick"] = "call(" + (e.Row.RowIndex + 1) + ");return false;";
        }
      
    }


    protected void showPics(string jobStatusId)
    {
       
        
    }

.ajax({

type: POST

url: 作业。 aspx / showPics

data: JSON .stringify({JobStatusId:i}),

contentType: application / json; charset = utf-8

dataType: json

成功:功能的pan>(){

alert(i);

},

错误:功能(){



alert( failed);

}

});
}
.ajax({ type: "POST", url: "Jobs.aspx/showPics", data: JSON.stringify({ JobStatusId: i }), contentType: "application/json; charset=utf-8", dataType: "json", success: function () { alert(i); }, error: function () { alert("failed"); } }); }



这是CS代码: -




And It is CS code:-

protected void gvJobStatus_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var lnkMap = (LinkButton)e.Row.FindControl("lnkOnMap");
            lnkMap.Attributes["onclick"] = "call(" + (e.Row.RowIndex + 1) + ");return false;";
        }
      
    }


    protected void showPics(string jobStatusId)
    {
       
        
    }


如果你想使用客户端脚本调用代码隐藏方法,那么请记住,方法应该是静态和web方法,所以只需编辑ShowPics方法的定义为



If you want to call code behind method using client side script, then keep thing in you mind that method should me static and web method, so just edit the definition of "ShowPics" methiod as

[WebMethod]
protected static void showPics(string jobStatusId)
    {
       
        //do your operation here
    }


[System.Web.Services.WebMethod]
   public static string showPics(string jobStatusId)
   {
       return jobStatusId;
   }







如果您得到答案,请标记为已回答..



谢谢



快乐编码....




if you get your answer then please marked as answered..

Thanks

Happy Coding....