【问题标题】:asp.net listbox double click event + event handlerasp.net 列表框双击事件 + 事件处理程序
【发布时间】:2010-12-17 09:47:24
【问题描述】:

我正在尝试在列表框中添加双击事件。

但我收到以下错误。

aspx 文件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBox__Test.aspx.cs" Inherits="DataBind__Various_Controls.ListBox__Test" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:ListBox ID="ListBox1" runat="server" Height="207px" Width="167px" AutoPostBack="True">
        </asp:ListBox>
    </div>

    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Button ID="btnForListBoxDoubleClick" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>

cs 文件

namespace DataBind__Various_Controls
{
    public partial class ListBox__Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string postbackRef = ClientScript.GetPostBackEventReference(btnForListBoxDoubleClick, "dblClickfromHiddenButton"); //this.Page.GetPostBackClientEvent(btnForListBoxDoubleClick, "dblClickfromHiddenButton");
                ListBox1.Attributes.Add("onclick", postbackRef);                                
            }
        }

        private void btnForListBoxDoubleClick_ServerClick(object sender, System.EventArgs e)
        {
            string argument = Request.Params["__EVENTARGUMENT"].Trim();

            if (argument == "dblClickfromHiddenButton")
            {
                this.Label1.Text = "Hello!";
            }
        }
    }
}

错误页面

Server Error in '/' Application.
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.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: 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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentException: 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.]
   System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +8620921
   System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +72
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +35
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053 

我该如何解决这个问题?

【问题讨论】:

    标签: asp.net listbox double-click


    【解决方案1】:

    有两种方法可以消除错误

    1.禁用事件验证

    这很简单,您只需在页面指令中设置 EnableEventValidation="false"。

    <%@ Page Language="C#" EnableEventValidation="false" EnableAutoEventWireup="true" CodeBehind="ListBox__Test.aspx.cs" Inherits="DataBind__Various_Controls.ListBox__Test" %>
    

    但微软不推荐它 (It is strongly recommended that you do not disable event validation. If you do disable event validation, make sure that no postback could be constructed that could have an unintended effect on your application.)

    2。注册您的活动

    您可以通过覆盖 Render 方法来注册您的客户端脚本。请注意,“渲染”是唯一允许您执行此操作的地方。

    protected override void Render(HtmlTextWriter writer)
    {
        Page.ClientScript.RegisterForEventValidation("btnForListBoxDoubleClick",  
                                                                "dblClickfromHiddenButton");
    }
    

    这应该可以消除 EnableEventValidation 错误,而不必禁用事件验证。

    希望这会有所帮助!

    附带说明,我不确定您是如何在页面中连接事件处理程序 btnForListBoxDoubleClick_ServerClick 的。这个事件将如何触发?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 2021-01-02
      • 2015-12-22
      • 2018-06-06
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      相关资源
      最近更新 更多