【问题标题】:How to get the CommandArgument of an ImageButton inside a Repeater如何在中继器中获取 ImageButton 的 CommandArgument
【发布时间】:2017-12-08 19:57:13
【问题描述】:

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

<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

的错误

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

【问题讨论】:

  • 你在哪里尝试这个? txtQty.Parent 是一个 div 而不是中继器项目,这就是你得到那个错误的原因

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


【解决方案1】:

要使用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"];
}

【讨论】:

  • 没有,但还有其他方法。我已经更新了我的答案。
猜你喜欢
  • 2011-12-18
  • 1970-01-01
  • 2021-10-24
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 1970-01-01
相关资源
最近更新 更多