当前位置:编程学习 > C#/ASP.NET >>

同样的代码为什么在母版页上出错?

想实现无刷新查询后台数据,在网上找到一些代码:
前台代码:
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <span id="king">
        <asp:DropDownList ID="drpList1" runat="server" Width="171px">
            <asp:ListItem>asa</asp:ListItem>
        </asp:DropDownList>
        </span>
        <a href="javascript:CallTheServer1('', king)">变更</a>
    </form>
</body>
</html>

后台代码:
public partial class WebForm3 : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
    {
        public string str = string.Empty;

        // Define method that processes the callbacks on server.
        public void RaiseCallbackEvent(String eventArgument)
        {
            str = eventArgument;
        }

        // Define method that returns callback result.
        public string GetCallbackResult()
        {
            string[] str = new string[] { "11", "22", "33", "44" };
            ListItem item = null;
            this.drpList1.Items.Clear();
            foreach (string tmp in str)
            {
                item = new ListItem(tmp, tmp);
                this.drpList1.Items.Add(item);
            }
            StringWriter writer1 = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
            HtmlTextWriter writer2 = new HtmlTextWriter(writer1);


            this.drpList1.RenderControl(writer2);
           

            writer2.Flush();
            writer2.Close();
            return  writer1.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScriptManager cs = Page.ClientScript;

            //定义客户端成功调用后的处理函数
            StringBuilder context1 = new StringBuilder();
            context1.Append("function ReceiveServerData1(arg, context)");
            context1.Append("{");
            context1.Append("context.innerHTML =  arg;");
            context1.Append("}");

            // 定义错误处理函数
            StringBuilder context2 = new StringBuilder();
            context2.Append("function ProcessCallBackError(arg, context)");
            context2.Append("{");
            context2.Append("alert('An error has occurred.');");
            context2.Append("}");

            //向客户端注册脚本
            cs.RegisterClientScriptBlock(this.GetType(), "ReceiveServerData1",
               context1.ToString(), true);
            cs.RegisterClientScriptBlock(this.GetType(), "ProcessCallBackError",
                context2.ToString(), true);

            //返回客户调用服务事件的脚本代码,结果如下:WebForm_DoCallback('__Page',arg,ReceiveServerData1,function ReceiveServerData1(arg, context){king.innerHTML =  arg;},ProcessCallBackError,false);
            //其中的content为king客户端king对象
            String cbReference1 = cs.GetCallbackEventReference("'" +
                Page.UniqueID + "'", "arg", "ReceiveServerData1", "context",
                "ProcessCallBackError", false);

            String callbackScript1 = "function CallTheServer1(arg, context) {" +
                cbReference1 + "; }";

            // 注册触发回调事件的脚本块
            cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer1",
                callbackScript1, true);
        }
    }

引用:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.IO;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

用在母版页上,提示错误!不在母版页上正常使用,请高手指点! --------------------编程问答-------------------- 母版页和一般的页面本身就不一样的吧,一个继承的System.Web.UI.MasterPage,一个继承的System.Web.UI.Page比如这段

WebForm_DoCallback('__Page',arg,ReceiveServerData1,function ReceiveServerData1(arg, context){king.innerHTML = arg;},ProcessCallBackError,false);
  //其中的content为king客户端king对象
  String cbReference1 = cs.GetCallbackEventReference("'" +
  Page.UniqueID + "'", "arg", "ReceiveServerData1", "context",
  "ProcessCallBackError", false);

里面的Page估计就有问题吧

--------------------编程问答-------------------- 那怎么处理啊?多谢指点! --------------------编程问答-------------------- --------------------编程问答-------------------- 自己顶顶! --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 一夜静悄悄! --------------------编程问答-------------------- 你将这些代码放入普通页面上,然后在public string GetCallbackResult()函数中调用母版页的代码就可以啦,不知道是否能满足你的需要? --------------------编程问答-------------------- 多谢指点,问题解决啦。但是,查询出来的数据,在后台不能找到也,怎么解决呢? --------------------编程问答-------------------- 咋没人回哪 --------------------编程问答-------------------- 放假啦,帖子也没人回了。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,