1

ItemTemplate 内にいくつかのフィールドを持つ asp リピーターがあります。リピーターの各アイテムには、「カートに追加」asp:ImageButton と非表示の asp:Label もあります。コードは次のようになります。

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

リピーターの特定の ImageButton がクリックされると、対応するラベルのテキストとして「カートに追加されました」を表示し、クリックされた ImageButton Visible=false を作成しようとしています。ASP:Repeater の OnItemCommand 関数を使用してみました。メソッドは「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; ;
    }


}

aspx ページは適切に設定されています。ただし、リピーターの ImageButtons のいずれかをクリックすると、次のエラーが発生します。

  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.

誰かがこれで私を助けることができますか?

4

1 に答える 1

0

サーバー側の呼び出しではなく、クライアント側の JavaScript でこれを行うことをお勧めしますか?

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

になる

<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" />
于 2012-04-16T23:43:46.360 に答える