3

我正在尝试从后面的代码向按钮添加 OnClick 属性。根据参加元素是否为 0,将确定添加哪个 OnClick 属性。当我单击带有以下代码的按钮时,出现以下错误:

“Microsoft JScript 运行时错误:Sys.WebForms.PageRequestManagerServerErrorException:回发或回调参数无效。使用配置中或页面中的 <%@ Page EnableEventValidation="true" %> 启用事件验证。出于安全目的,此功能会验证该参数回发或回调事件源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法注册回发或回调数据以进行验证。

我究竟做错了什么?

ASPX

<%@ Page Title="" Language="C#" EnableEventValidation="true" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="test.CommunityEvents.Default" %>
 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:DataList ID="DataList1" RepeatColumns="1" CellPadding="5" OnItemDataBound="Dl1_ItemDataBound"
            runat="server">
            <ItemTemplate>
                   <div id="Attendingbox" runat="server">
                       <asp:Label ID="AttendingorNot" runat="server"></asp:Label>
                    </div>
                    <br />
                    <asp:Button ID="SignupButton" runat="server" Text="" />
            </ItemTemplate>
        </asp:DataList>
    </ContentTemplate>
</asp:UpdatePanel>

代码背后

protected void Dl1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            .....//removed other code to save space

            Button SignupButton = (Button)e.Item.FindControl("SignupButton");
            if (Attending == 0)
            {
                AttendingorNot.Text = "You are not attending";
                AttendingorNot.Attributes.Add("class", "alert");
                SignupButton.Text = "Attend";
                SignupButton.Attributes.Add("class", "btn btn-large btn-success");
                SignupButton.Click += new EventHandler(Submit_Add);
            }
            else
            {
                AttendingorNot.Text = "You are attending!";
                AttendingorNot.Attributes.Add("class", "alert alert-success");
                SignupButton.Text = "Remove";
                SignupButton.Attributes.Add("class", "btn btn-large btn-danger");
                SignupButton.Click += new EventHandler(Submit_Remove);
            }
        }
    }
    private void Submit_Remove(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?msg=work");
    }
    private void Submit_Add(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?msg=gone");
    }
4

3 回答 3

9

使用以下代码

Button1.Attributes.Add("OnClick","btn_Click");

或者

Button1.Click += new EventHandler(btn_Click);

这是按钮点击方法

protected void btn_Click(object sender, EventArgs e)
{
   do anything...        
}
于 2015-01-19T11:23:30.657 回答
1

如果你想动态附加按钮的事件,你想使用按钮的CommandName,并在ItemCommand事件中捕获事件。

主要优点是您可以捕获单击了哪一行。

protected void Dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
       e.Item.ItemType == ListItemType.AlternatingItem)
    {
        .... 
        Button SignupButton = (Button)e.Item.FindControl("SignupButton");
        if (Attending == 0)
        {
            SignupButton.Text = "Attend";
            SignupButton.Attributes.Add("class", "btn btn-large btn-success");
            SignupButton.CommandName = "Attend";
        }
        else
        {
            SignupButton.Text = "Remove";
            SignupButton.Attributes.Add("class", "btn btn-large btn-danger");
            SignupButton.CommandName = "Remove";
        }
    }
}

protected void Dl1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "Attend")
    {
        Response.Redirect("Default.aspx?msg=work");
    }
    else if (e.CommandName == "Remove")
    {
        Response.Redirect("Default.aspx?msg=gone");
    }
}
于 2013-09-27T15:11:07.107 回答
-2

用这个

AddHandler btn.Click, AddressOf [FunktionName]
于 2014-06-23T08:56:01.167 回答