如何在同一个onclick事件上调用客户端和服务器端功能? [英] How to call Client Side and Server side function on same onclick event?

查看:118
本文介绍了如何在同一个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

成功:功能(){

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....


这篇关于如何在同一个onclick事件上调用客户端和服务器端功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆