如何在Repeater

时间:2017-07-05 08:48:37

标签: asp.net repeater imagebutton asp.net-2.0 commandargument

我有一个像这样结构的转发器

<asp:Repeater ID="RpAccBind" runat="server" OnItemDataBound="RpAccBind_ItemDataBound">
    <ItemTemplate>
        <tr id="acsryRow" runat="server">
            <td data-th="Product">
                <div class="prodImgMain">
                    <img src="../instrumentimages/<%# Eval("ProductImage") %>" alt="<%# Eval("ProductName")%>" />
                </div>
            </td>

            <td data-th="Description">
                <div class="prodDescriptionContainer">
                    <ul>
                        <li>
                            <span class="prdRow"><%# Eval("ProductName") %></span>
                        </li>
                    </ul>
                </div>
                <div class="quantityIconWrap form-group">
                    <span class="number-wrapper">

                        <asp:TextBox Text='<%# Eval("Quantity") %>' ID="txtqty" CssClass="form-control" runat="server" size="3" Width="50" onkeyup="this.value=this.value.replace(/[^0-9]/g,'');" OnTextChanged="txtQtyTextChanged" AutoPostBack="true"></asp:TextBox>

                    </span>
                    <span>

                        <asp:ImageButton ID="lnkAscRemove" runat="server" class="btn" OnClick="lnkRemoveClick" CommandArgument='<%# Eval("ProductId")%>' ImageUrl="../images/deleteIcon.png"></asp:ImageButton>
                    </span>
                </div>
            </td>

            <td data-th="Amount" style="padding-right: 5%;">
                <div class="amountColMain">
                    <ul>
                        <li><span><strong><span id="litConPrice" runat="server">$<%# Eval("ConsumerPrice") %></span></strong></span></li>

                    </ul>
                </div>
            </td>
            <td></td>
        </tr>

    </ItemTemplate>
    <FooterTemplate>
    </FooterTemplate>
</asp:Repeater>

现在,每当txtQty为0时,我想要点击图像按钮的productid。现在,我正在使用像

这样的东西
long productId = Convert.ToInt64(((ImageButton)((RepeaterItem)txtQty.Parent).FindControl("lnkAscRemove")).CommandArgument);

我错误地将 HtmlTableCell 强制转换为 RepeaterItem

任何形式的帮助将不胜感激。提前致谢

1 个答案:

答案 0 :(得分:0)

要使用CommandNameCommandArgument,您需要使用Command个活动,而不是Click

<asp:ImageButton ID="lnkAscRemove" runat="server" OnCommand="lnkAscRemove_Command"
    CommandArgument='<%# Eval("ProductId")%>'

背后的代码

protected void lnkAscRemove_Command(object sender, CommandEventArgs e)
{
    Label1.Text = e.CommandArgument.ToString();
}

<强>更新

如果您想从TextBox获取ProductId,可以将其添加为自定义属性,然后在后面的代码中读取它。

<asp:TextBox Text='<%# Eval("ProductId") %>' ID="txtqty" runat="server" AutoPostBack="true"
    OnTextChanged="txtqty_TextChanged" ProductId='<%# Eval("ProductId") %>'></asp:TextBox>

背后的代码

protected void txtqty_TextChanged(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;
    Label1.Text = tb.Attributes["ProductId"];
}