如何在Repeater中的下拉列表更改时触发事件

时间:2012-11-07 10:45:57

标签: c# asp.net events

当用户更改下拉列表的selectedIndex时,我想在我的数据库中执行一些操作。现在我有以下内容。

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

我想要的只是在changeCount方法中获取隐藏字段值。问题是我无法直接获取隐藏字段值,因为此代码位于Repeater元素中。我怎样才能实现这一功能?

2 个答案:

答案 0 :(得分: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"));    
      }
}

答案 1 :(得分:1)

您可以将GiftVoucher.ID值绑定到DropDown的自定义属性并跳过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"];
}