English 中文(简体)
asp.net listbox double click event + event handler
原标题:

I am trying to add a double-click event in a listbox.

But I am getting the following error.

the aspx file

<%@ 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>

the cs file

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!";
            }
        }
    }
}

the error page

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 

How can I solve the problem?

最佳回答

There are two ways you can get rid of the error

1. Disable Event Validation

This is straightforward and all you need to do is set EnableEventValidation="false" in the page directive.

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

It is however not recommended by Microsoft (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. Register Your Event

You can register your client script by overriding the Render method. Note that "Render" is the only place you are allowed to do this.

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

This should get rid of the EnableEventValidation error without having to disable event validation.

Hope this helps !

As a side note, I am not sure how you are hooking up the event handler btnForListBoxDoubleClick_ServerClick in your page. How is this event going to be triggered ?

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签