2

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?

4

2 に答える 2

6
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"));    
      }
}
于 2012-11-07T11:00:07.170 に答える
2

値を DropDown のカスタム属性にバインドGiftVoucher.IDして、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"];
}
于 2012-11-07T11:30:27.793 に答える