1

I have a repeater which structured like this

<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>

Now, whenever the txtQty is 0, i want to have the productid in respect of the imagebutton clicked. Right Now, i am using something like

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

It is giving me an error of unable to cast HtmlTableCell to RepeaterItem

Any Kind of help will be appreciated. Thanks in advance

VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • where are you trying this ? txtQty.Parent is a div not repeater item, that's why you got that error – Krishna Jul 05 '17 at 09:14

2 Answers2

2

To use CommandName and CommandArgument, you need to use a Command event, not a Click.

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

Code behind

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

Update

If you want to get the ProductId from a TextBox, you could add it as a custom property and then read it in code behind.

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

Code behind

protected void txtqty_TextChanged(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;
    Label1.Text = tb.Attributes["ProductId"];
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
0

The Repeater has an ItemCommand that watches for button events inside the Repeater elements. The CommandName and CommandArgument is passed to the event.

Also of importance is the Item which represents the Repeater Item that the button was clicked in. Then you can use the FindControls method to find related server controls object for the same RepeaterItem in which the button was clicked.

protected void MyRepeater_ItemCommand(Object source, RepeaterCommandEventArgs e) 
{
   if (e.CommandName == "edit") {
       SetEditorTo(e.CommandArgument);
   }
   RepeaterItem row = e.Item;
   Textbox tQty = row.FindControls("txtqty");
}
     
Stephen Wrighton
  • 36,783
  • 6
  • 67
  • 86