【问题标题】:Datasource for dropdown overridding static value?下拉覆盖静态值的数据源?
【发布时间】:2012-05-05 05:19:50
【问题描述】:

我正在使用数据绑定到数据源的下拉服务器控件,但我还希望将默认值(“选择程序”)硬编码为列表的第一项。

代码隐藏

/// <summary>
/// Page Load
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
    ddPrograms.DataSource = Programs.SelectProgramID(1);
    ddPrograms.DataBind();        
}

前端

  <asp:DropDownList ID="ddPrograms" runat="server" DataValueField="ProgramID" DataTextField="Name" AutoPostBack="true">
            <asp:ListItem Text="Select a program" Value="0" Selected="True"></asp:ListItem>   
        </asp:DropDownList>

【问题讨论】:

    标签: asp.net servercontrols


    【解决方案1】:

    尝试在 DropDownList 上设置AppendDataBoundItems="true"

    <asp:DropDownList ID="ddPrograms" runat="server" DataValueField="ProgramID" DataTextField="Name" AutoPostBack="true" AppendDataBoundItems="true">
            <asp:ListItem Text="Select a program" Value="0" Selected="True"></asp:ListItem>   
        </asp:DropDownList>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 2018-06-21
      • 1970-01-01
      相关资源
      最近更新 更多