6

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

sebkrueger
  • 386
  • 5
  • 16
Koray Durudogan
  • 624
  • 4
  • 12
  • 31
  • Well, It doesn't seems to have a problem with the code!! may be it could be becuase of the name you should try with `Click_` and lets see what happens – Just code Jun 02 '14 at 13:31
  • Check for syntax errors on your button line, namely your command argument. Sorry I'm not more helpful, I just know I used to do a lot of these and oftentimes syntax errors can cause weird errors to pop up. It is very frustrating, but you might try rewriting this if you can't find any other error. – Musicode Jun 02 '14 at 13:35

2 Answers2

1

I solved the problem and wanted to write here, so i can help anyone with the same problem.Adding an "if (!IsPostBack)" under the pageload is solving the problem.Thanks for the all help.

Koray Durudogan
  • 624
  • 4
  • 12
  • 31
0

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
}
V319
  • 150
  • 1
  • 13