关于Asp.Net DataList命令和EventValidation

时间:2008-11-14 12:37:07

标签: asp.net event-handling datalist

我有一个更新面板内的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.

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

我在这里缺少什么?

1 个答案:

答案 0 :(得分: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
}