2

当用户更改下拉列表的 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方法中获取我的隐藏字段值。问题是我不能直接获取隐藏字段的值,因为这段代码在Repeaterelement. 我怎样才能实现该功能?

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

您可以将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"];
}
于 2012-11-07T11:30:27.793 回答