1

I've an asp repeater which has some fields inside an ItemTemplate. Each item in the repeater has an "add to cart" asp:ImageButton and an invisible asp:Label as well. The code looks like this:

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="addToCart">
        <HeaderTemplate>
            <table id="displayTable"> 
        </HeaderTemplate>
        <ItemTemplate>
           <td>
                <!-- fields like name, description etc in the repeater are present; i've omitted to show them here-->
                <asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>       
                <asp:ImageButton ID="addToCartImg" runat="server" ImageUrl="hi.jpg" Width="75px" Height="50px" />
           </td>
        </ItemTemplate>
        <FooterTemplate>
           </table>
        </FooterTemplate>
</asp:Repeater>

When a particular ImageButton in the repeater is clicked, I'm trying to display "added to cart" as the text of its corresponding Label, and make the clicked ImageButton Visible=false. I've tried using the OnItemCommand function for the ASP:Repeater. The method is "addToCart": <>

void addToCart(Object Sender, RepeaterCommandEventArgs e)
{
    Cart cart = new Cart();
    cart.instrument_id = //id of product from repeater based on user click
    String userName = Membership.GetUser().ToString();
    cart.user_name = userName;
    cart.quantity = 1;
    var thisLbl = (Label)e.Item.FindControl("addedToCartLabel");
    var thisImg = (ImageButton)e.Item.FindControl("addToCartImg");
    try
    {
        database.Carts.InsertOnSubmit(cart);
        database.SubmitChanges();

        thisImg.Visible = false;
        thisLbl.Text = "Added to Cart!";
        thisLbl.Visible = true;

    }
    catch (Exception ex)
    {
        thisImg.Visible = false;
        thisLbl.Text = "Processing failed;please try again later";
        thisLbl.Visible = true; ;
    }


}

The aspx page is populated properly. However, when I click on any of the ImageButtons in the repeater, I get the following error:

  Server Error in '/mysite' Application.
    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.

Can someone help me with this?

codewarrior
  • 984
  • 4
  • 18
  • 34

1 Answers1

0

May I suggest doing this with client-side javascript rather than a server-side call?

<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>       
<asp:ImageButton ID="addToCartImg" runat="server" ImageUrl="hi.jpg" Width="75px" Height="50px" />

becomes

<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>       
<asp:ImageButton ID="addToCartImg" runat="server" onclick="javascript:function() { this.this.style.display='none'; document.getElementById(this.parentNode.firstChild.id).style.display='block'; }"  ImageUrl="hi.jpg" Width="75px" Height="50px" />
Evan
  • 2,113
  • 1
  • 20
  • 24
  • I guess that would work, but I need to insert a record into the Cart table when the user clicks on the imagebutton. I've updated my the addToCart method above. I thought this questio would make me learn how to access a particular Item in the Repeater so that I can store the clicked product in the database. Sorry for not elaborating earlier. – codewarrior Apr 17 '12 at 00:01
  • 1
    Okay... is the repeater wrapped in an update panel? I think you might want to take a look at this question/accepted answer: http://stackoverflow.com/questions/228969/invalid-postback-or-callback-argument-event-validation-is-enabled-using-page – Evan Apr 17 '12 at 18:46