Chuck Norris
Chuck Norris

Reputation: 15190

How to fire an event when dropdown inside Repeater changes

I want to do some actions in my database when user change dropdown's selectedIndex.Now I have the following.

<td class="shop-item-qty">
<asp:DropDownList ID="qtyDropDownList" OnSelectedIndexChanged="changeCount" AutoPostBack="true"  runat="server"/>
<asp:HiddenField ID="ItemId" runat="server" Value='<%#Eval("GiftVoucher.ID") %>'/>
</td>

All I want is to get my hidden fields value in changeCount method. The problem is that I can't directly get hidden fields value, because this code is in Repeater element. How can I achieve that functionality?

Upvotes: 2

Views: 1783

Answers (2)

IUnknown
IUnknown

Reputation: 22458

You can bind GiftVoucher.ID value to DropDown's custom attribute and skip HiddenField:

<asp:DropDownList runat="server" ID="qtyDropDownList" OnSelectedIndexChanged="changeCount" AutoPostBack="true" data-itemId='<%# Eval("ID") %>' />

protected void changeCount(object sender, EventArgs e)
{
    var id = ((DropDownList)sender).Attributes["data-itemId"];
}

Upvotes: 2

Pankaj Agarwal
Pankaj Agarwal

Reputation: 11309

protected void qtyDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
      DropDownList control = (DropDownList)sender;

      RepeaterItem rpItem = control.NamingContainer as RepeaterItem;
      if (rpItem != null)
      {
         HiddenField hiddenField = ((HiddenField)rpItem.FindControl("ItemId"));    
      }
}

Upvotes: 6

Related Questions