WebFrom前后台方法调用

1.后台调用前台JS方法(带参数)

ClientScript.RegisterStartupScript(类型,键,调用的JS方法名(+参数+),是否启用该方法(true or flase));

例:ClientScript.RegisterStartupScript(typeof(Page), "aa", "SelectLeftPage(" + nRoleId +  ")", true);

2.前台调用后台方法

方法有两种:一种是Ajax方法,另一种是AjaxPro

Ajax异步刷新

前台

$.ajax({
                type: "POST",                             //提交方式
                url: "ProjectBasicEidt.aspx/showMineData",  //页面URL和方法
                data: "{strMineCode:'" + params + "'}",      //参数,若无参写null
                dataType: "json",                        //类型
                contentType: "application/json; charset=utf-8",//必须使用,如果不使用就会出现乱码
                success: function (Msg) {
                    document.getElementById("lblMineName").innerHTML = Msg.d[0]; //前台Label赋值(只可展示,后台无法获取其值)
                    $("#hidMineCode").val(Msg.d[1]);  //hidden赋值 
                }
            });

后台

(注意:1.参数一定要对应。2.调用方法需要加契约“[System.Web.Services.WebMethod()]”。3.调用方法一定为静态的。4.项目必须为.netFramework2.0以上的版本)

/// <summary>
        /// 获取信息,返回数组
        /// </summary>
        [System.Web.Services.WebMethod()]
        public static Array showMineData(string strMineCode)
        {
            string[] strMineInfo = new string[5];

            strMineInfo[0] = NAME;

            return strMineInfo;
        }

原文地址:https://www.cnblogs.com/hjxh/p/3803704.html