About DataList Asp.Net and EventValidation Commands

I have a Datalist that is inside the update panel and is in the panel in modalpopupextender;

I can list the items as I wanted. I also put 2 buttons for delete and AddBelow. Here's the markup:

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

      

And register evenst like below: (I used both ItemCommand and DeleteCommand to see which works :)

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

      

But when I click the Delete or Add buttons, I get the following error:

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.

      

I disabled the eventvalidation of the page, but the event could not be caught ...

What am I missing here?

+1


source to share


1 answer


The control registers its events during rendering and then checks for events during feedback or callback processing. You log events manually during Page_Load.

Try refactoring your code to specify an event handler on the .aspx page, bind the element id to the CommandArgument button, and then get the associated element id in the handler. I also had separate event handlers for different events, this is a little cleaner. Something like:

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

      

0


source







All Articles