在一个项目中所有的Ajax功能用一个页面来实现


在一个项目中用到了ajax异步,需要由一个aspx页面的pageload事件来读取后台数据,如果有多少个ajax功能就写多少个页面,不可取,可用一个页面来实现,代码如下:ajaxPage.aspx页面代码:

 protected void Page_Load(object sender, EventArgs e)
        {
            string Action = Xkzi.Common.RequestHelp.Q("Action");
            switch (Action)
            {
                case "ChackPass":
                    {
                        //得到传过来的密码的字符串
                        string Pass = Tools.GetMD5(Request.QueryString["Pass"].ToString());
                        //得到当前用户的ID
                        //string CurrentUserId = Session["CurrentUserId"].ToString();
                        string CurrentUserId = CurrentPatient.Id.ToString();
                        //进行判断,看传过来的用户ID和密码是否匹配
                        DataSet ds = new DataSet();
                        if (Business.Patient.CheckPass(Convert.ToInt32(CurrentUserId), Pass))
                        {
                            Response.Write("旧密码验证成功");
                        }
                        else
                        {
                            Response.Write("旧密码验证失败");
                        }
                        break;
                    }
                case "Display":
                    {
                        //接到url传来的字符
                        string DicTitle = Request.QueryString["Dic_Title"].ToString();
                        //解码
                        DicTitle = System.Web.HttpUtility.UrlDecode(DicTitle, System.Text.Encoding.GetEncoding("GB2312"));
                        //得到数据源
                        DataSet ds = new DataSet();
                        ds = Business.Dictionary.GetList(" Dic_Title='" + DicTitle + "'");
                        string a = ds.Tables[0].Rows[0]["Dic_Content"].ToString();
                        //输出想要的结果
                        Response.Write(ds.Tables[0].Rows[0]["Dic_Content"].ToString());
                        break;
                    }
            }
              

        }

这段代码包括了两个ajax功能,一个是页面的滑词搜索,另一个是密码验证

在那两个功能的JS文件也要做一些小的修改

 xmlhttp.open("GET","AjaxPage.aspx?Action=Display&Dic_Title="+encodeURI(keys),true);

其实就是加多传一个参数,然后在ajaxPage.aspx页面里先判断传入的Action的值是什么,就可以确定是要用哪个功能了


转载自:http://www.cnblogs.com/yuanweisen/archive/2008/12/03/1346993.html#1402950

原文地址:https://www.cnblogs.com/KingStar/p/1631623.html