views:

1258

answers:

1

I have Datalist that is inside an updatepanel and it is in panel in modalpopupextender;

I can list items as I wanted. I am also putting 2 buttons nfor Delete and AddBelow. Here is 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 following: (I have 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 Delete Or AddBelow buttons I get following error:

Invalid postback or callback argument. Event validation is enabled using 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 have disabled eventvalidation of page but the event couldn't be catched...

What am I missing here?

A: 

A control registers its events during rendering and then validates the events during the post-back or callback handling. You're registering the events manually during Page_Load.

Try refactoring your code so you specify the event handler in the .aspx page, bind the item's Id to the button's CommandArgument, then get the bound item Id in the handler. I would also have separate event handlers for the different events, it's 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
}
flesh