在ASP.NET2.0中我使用Callback技术从服务器端取得数据,并用JS添加到DropDownList控件中
但提交时出现 invaild Postback 错误 ,因为DropDownList被JS改动过,出于安全性问题,肯定会错
系统提示要使用ClientScript.RegisterForEventValidation,但我不知如何使用,还请高手赐教,先谢了,全代码如下
aspx代码
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Server Time</title> 
      <script language="javascript">          function GetServerTime() 
         { 
            var message = ''; 
            var context = ''; 
            
            <%=sCallBackFunctionInvocation%> 
         } 
         
         function ShowServerTime(timeMessage, context) 
        { 
            var oOption = document.createElement("OPTION");
            oOption.text=timeMessage;
            oOption.value=timeMessage;
            document.all("DropDownList1").add(oOption);         } 
      </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" value="得到服务器端时间" onclick="GetServerTime();" id="Button1" />    
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /></div>
    </form>
</body>
</html>

解决方案 »

  1.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
    {
        public string sCallBackFunctionInvocation;
        public string sTime;    protected void Page_Load(object sender, EventArgs e)
        {
         sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "ShowServerTime", "context");            
        }    #region ICallbackEventHandler Members    public string GetCallbackResult()
        {
            return sTime;
        }    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
        {
            sTime = DateTime.Now.ToString();
        }    #endregion
        protected void Button2_Click(object sender, EventArgs e)
        {
         /*
         点此按钮时就出错,无法提交,出现如下错误
         Invalid postback or callback argument.  
         Event validation is enabled using <pages enableEventValidation="true"/> in configuration 
         or <%@ Page EnableEventValidation="true" %> in a page.  
         For security purposes, 
         this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  
         If the data is valid and expected, 
         use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. 
         */
            if (DropDownList1.Items.Count > 0)
            {
                Response.Write(DropDownList1.SelectedItem.ToString());            
            }
        }
    }