Domanda

Ho un Datalist che si trova all'interno di un pannello di aggiornamento ed è nel pannello in modalpopupextender;

Posso elencare gli elementi come volevo. Metto anche 2 pulsanti per Delete e AddBelow. Ecco il 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>

E registrati anche come segue: (Ho usato sia ItemCommand che DeleteCommand per vedere quale funziona :)

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

Ma quando faccio clic sui pulsanti Elimina o AggiungiBelow visualizzo il seguente errore:

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.

Ho disabilitato la convalida dell'evento della pagina ma l'evento non è stato intercettato ...

Cosa mi sto perdendo qui?

È stato utile?

Soluzione

Un controllo registra i suoi eventi durante il rendering e quindi convalida gli eventi durante la gestione di postback o callback. Stai registrando gli eventi manualmente durante Page_Load.

Prova a eseguire il refactoring del codice in modo da specificare il gestore eventi nella pagina .aspx, associare l'ID dell'articolo al CommandArgument del pulsante, quindi ottenere l'ID elemento associato nel gestore. Avrei anche gestori di eventi separati per i diversi eventi, è un po 'più pulito. Qualcosa del tipo:

<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
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top