【问题标题】:Change value of label and hide imagebutton in asp repeater在 asp 中继器中更改标签的值并隐藏图像按钮
【发布时间】:2012-04-16 23:32:43
【问题描述】:

我有一个 asp 中继器,它在 ItemTemplate 中有一些字段。转发器中的每个项目都有一个“添加到购物车”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.

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: c# asp.net .net linq repeater


    【解决方案1】:

    我可以建议使用客户端 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" />
    

    【讨论】:

    • 我想这会起作用,但是当用户单击图像按钮时,我需要在 Cart 表中插入一条记录。我已经更新了上面的 addToCart 方法。我认为这个问题会让我学习如何访问中继器中的特定项目,以便我可以将点击的产品存储在数据库中。抱歉没有早点详细说明。
    • 好的...中继器是否包含在更新面板中?我想你可能想看看这个问题/接受的答案:stackoverflow.com/questions/228969/…
    猜你喜欢
    • 2012-09-17
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多