2012-04-16 77 views
1

我有一個在ItemTemplate中有一些字段的asp中繼器。中繼器中的每個項目都有一個「添加到購物車」asp:ImageButton和一個不可見的asp:標籤。代碼如下所示:在asp中繼器中更改標籤的值並隱藏imagebutton

<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可見=假。我已經嘗試使用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頁面正確填充。

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. 

有人可以幫助我:但是,在轉發時,我得到以下錯誤,當我點擊任何ImageButtons的?

回答

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" /> 
+0

我想這會的工作,但我需要插入一條記錄到車表當用戶點擊的ImageButton。我已經更新了上面的addToCart方法。我認爲這個問題會讓我學習如何訪問Repeater中的特定項目,以便我可以將點擊的產品存儲在數據庫中。對不起,沒有詳細說明。 – codewarrior 2012-04-17 00:01:20

+1

好的...是包裹在更新面板中的中繼器嗎?我想你可能想看看這個問題/接受的答案: http://stackoverflow.com/questions/228969/invalid-postback-or-callback-argument-event-validation-is-enabled-using-頁 – Evan 2012-04-17 18:46:42