将默认值添加到asp.net中的dropdownlist

时间:2017-07-27 07:25:32

标签: c# asp.net

我有一个绑定到数据库的下拉列表。我想添加数据库中不存在的其他列表项。我能做些什么呢? 感谢

我的下拉列表:

<asp:DropDownList ID="dpl_goalGallery" runat="server" CssClass="axmor-form-field selectType" AutoPostBack="True" Font-Size="14px" DataSourceID="ds_sourceGallery" DataTextField="galleryTitle" DataValueField="galleryId">
    <asp:ListItem>select...</asp:ListItem>
    </asp:DropDownList>

    <asp:SqlDataSource ID="ds_goalGallery" runat="server" ConnectionString="<%$ ConnectionStrings:anaconnection %>" SelectCommand="sp_select_gallery" SelectCommandType="StoredProcedure">
       <SelectParameters>
           <asp:Parameter DefaultValue="0" Name="act" Type="Int32" />
           <asp:Parameter DefaultValue=" " Name="fromDate" Type="String" />
           <asp:Parameter DefaultValue=" " Name="toDate" Type="String" />
       </SelectParameters>
    </asp:SqlDataSource>
</asp:DropDownList>

1 个答案:

答案 0 :(得分:0)

您可以在数据绑定期间执行此操作:

dpl_goalGallery.Items.Insert(0, new ListItem("Select","NA")

或者您可以将默认标记添加为

<asp:DropDownList .. AppendDataBoundItems="true">
   <Items>
       <asp:ListItem Text="Select" Value="" />
   </Items>
</asp:DropDownList>
相关问题