1

我有一个更新面板内的 Datalist,它位于 modalpopupextender 的面板中;

我可以列出我想要的项目。我还为 Delete 和 AddBelow 放置了 2 个按钮。这是标记:

<asp:DataList ID="SpeedDialsDL" runat="server">
                <ItemTemplate>
                    <table id="speedDialValueEditorTable" width="100%">
                        <tr>
                            <td width="275px">
                                <asp:Label ID="ValueLabel" runat="server" Text="Value"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="ValueTextBox" runat="server" Text='<%# Eval("Value") %>' Width="340px"
                                    Enabled="false"></asp:TextBox>
                            </td>
                        </tr>                       
                        <tr>
                            <td colspan="2">
                                <asp:Button ID="Delete" runat="server" Text="Delete" CommandName="Delete"
                                 CausesValidation="false" />&nbsp;
                                <asp:Button ID="AddNewButton" runat="server" Text="AddBelow" CommandName="AddBelow"
                                   CausesValidation="false" />
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>

并注册如下事件:(我同时使用了 ItemCommand 和 DeleteCommand 来查看哪个有效:)

protected void Page_Load(object sender, EventArgs e)
{
    SpeedDialsDL.ItemCommand += SpeedDialsDL_ItemCommand;
    SpeedDialsDL.DeleteCommand += SpeedDialsDL_ItemCommand;            
}        

void SpeedDialsDL_ItemCommand(object source, DataListCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "Delete":
            this.DeleteFromList((string)e.CommandArgument);
            break;
        case "AddBelow":
            break;
    }
}

但是当我单击 Delete 或 AddBelow 按钮时,我收到以下错误:

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.

我已禁用页面的事件验证,但无法捕获该事件...

我在这里想念什么?

4

1 回答 1

0

控件在呈现期间注册其事件,然后在回发或回调处理期间验证事件。您在 Page_Load 期间手动注册事件。

尝试重构代码,以便在 .aspx 页面中指定事件处理程序,将项的 Id 绑定到按钮的 CommandArgument,然后在处理程序中获取绑定的项 Id。对于不同的事件,我也会有单独的事件处理程序,它更简洁一些。就像是:

<asp:DataList ID="SpeedDialsDL" runat="server">                
<ItemTemplate>                    
    <table id="speedDialValueEditorTable" width="100%">                        
        <tr>                            
            <td width="275px">                                
                <asp:Label ID="ValueLabel" runat="server" Text="Value"></asp:Label>                            
            </td>                            
            <td>                                
                <asp:TextBox ID="ValueTextBox" runat="server" Text='<%# Eval("Value") %>' Width="340px" Enabled="false"></asp:TextBox>                            
            </td>                       
        </tr>                                               
        <tr>                            
            <td colspan="2">                                
                <asp:Button ID="Delete" runat="server" Text="Delete" OnClick="Delete" CausesValidation="false" CommandArgument='<%# Eval("Id") %>' />
                <asp:Button ID="AddNewButton" runat="server" Text="AddBelow" OnClick="AddBelow" CausesValidation="false" CommandArgument='<%# Eval("Id") %>' />                            
            </td>                     
        </tr>                   
    </table>                
</ItemTemplate>            

protected void Delete(object sender, EventArgs e)
{
    Button b = (Button)sender as Button;
    string boundItemId = b.CommandArgument;
    //handle delete
}

protected void AddBelow(object sender, EventArgs e)
{
    Button b = (Button)sender as Button;
    string boundItemId = b.CommandArgument;
    //handle add below
}
于 2008-11-15T09:44:06.743 回答