简体   繁体   中英

repeater item command in asp.net

i am working with asp.net and i have a repeater.I want to take a respond from one of the items, which has been displayed by repeater.Here is my code:

<asp:Repeater ID="Repeatercart" runat="server" OnItemCommand ="RepeaterDeleteitemcommand" >
<ItemTemplate>                     
    <table> 
    <tr>
        <td><img id="Image1" src="PerfumeImages/<%#Eval("ProductImage") %>" width="90" /></td>
    </tr>                               
    <tr>
        <td><%#Eval("ProductName") %> x<%#Eval("Quantity") %></td>
    </tr>
    <tr>
        <td> <%#Eval("ProductGender") %></td>
    </tr>
    <tr>
        <td> <%#Eval("ProductSize") %> ml</td>
    </tr>                               
    <tr>
        <td><a class="buy-btn"><asp:Button ID="Button1" CommandName="Click" Text="Remove from cart" runat="server" CommandArgument='<%# Eval("ProductId") %>' /></a></td>
    </tr>
    </table>            
</ItemTemplate>
</asp:Repeater>   

and this is my codebehind:

protected void RepeaterDeleteitemcommand(object source, RepeaterCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "Click":
               Label1.Text = "ok !";
               break;
    }
}  

When i run this code, i get an error like: 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.Thanks for the help

我解决了这个问题并想写在这里,所以我可以帮助任何有同样问题的人。在pageload下添加一个“if(!IsPostBack)”解决了这个问题。谢谢你的帮助。

Try to use something like this

<asp:LinkButton runat="server" ID="btnDelete" Text="Delete" OnCommand="btnDelete_Click" CommandArgument="<%# ((YourObjType)Container.DataItem).Id %>"

So

protected void btnDelete_Click(object sender, CommandEventArgs e)
{
   //e.CommandArgument contains your id
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM