Skip to main content
 首页 » 编程设计

c#之如何使用 C# 绑定(bind) .aspx 中定义的现有下拉列表

2024年02月06日14arxive

我在设计页面中有一个下拉列表,如下所示:

<asp:DropDownList ID="ddlArtList" runat="server"> 
  <asp:ListItem Value="95">Select</asp:ListItem> 
  <asp:ListItem Value="1">1</asp:ListItem> 
  <asp:ListItem Value="2">2</asp:ListItem> 
  <asp:ListItem Value="3">3</asp:ListItem> 
  <asp:ListItem Value="4">4</asp:ListItem> 
  <asp:ListItem Value="5">5</asp:ListItem> 
  <asp:ListItem Value="6">6</asp:ListItem> 
</asp:DropDownList> 

根据需要,上述项目有时会被 C# 中的某些其他值覆盖。 但最后我想在 C# 的帮助下绑定(bind)上面的默认项以获取上面的列表项。

我想知道在 C# 中是否有任何内置方法或属性来绑定(bind)下拉列表 (.aspx)。

不使用这个:ddlArtList.Items.Add ("1); 等等。

提前致谢。

请您参考如下方法:

使用AppendDataBoundItems

.aspx代码:

<asp:DropDownList ID="ddlArtList" AppendDataBoundItems="True" runat="server"> 
  <asp:ListItem Value="95">Select</asp:ListItem> 
  <asp:ListItem Value="1">1</asp:ListItem> 
  <asp:ListItem Value="2">2</asp:ListItem> 
  <asp:ListItem Value="3">3</asp:ListItem> 
  <asp:ListItem Value="4">4</asp:ListItem> 
  <asp:ListItem Value="5">5</asp:ListItem> 
  <asp:ListItem Value="6">6</asp:ListItem> 
</asp:DropDownList> 

服务器端:

ddlArtList.AppendDataBoundItems="True"