代码之家  ›  专栏  ›  技术社区  ›  yusuf

关于ASP.NET数据列表命令和事件验证

  •  1
  • yusuf  · 技术社区  · 16 年前

    我有一个数据列表,在一个更新面板内,它在modalpopupExtender的面板中;

    我可以列出我想要的物品。我还在下面放置两个用于删除和添加的按钮。标记如下:

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

    但当我单击“删除”或“添加”按钮时,会出现以下错误:

    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.
    

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

    我这里缺什么?

    1 回复  |  直到 12 年前
        1
  •  0
  •   flesh    16 年前

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

    尝试重构代码,以便在.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
    }