1

我有一个结构像这样的中继器

<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 时,我都希望点击 imagebutton 的 productid。现在,我正在使用类似的东西

long productId = Convert.ToInt64(((ImageButton)((RepeaterItem)txtQty.Parent).FindControl("lnkAscRemove")).CommandArgument);

它给了我一个无法将HtmlTableCell转换为RepeaterItem的错误

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

4

1 回答 1

1

要使用CommandNameand CommandArgument,您需要使用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();
}

更新

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

<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"];
}
于 2017-07-05T08:58:52.557 回答