jh_6990 jh_6990 - 3 years ago 110
ASP.NET (C#) Question

add default value to dropdownlist in asp.net

i have dropdownlist that binded to database. i want to add other list item that don't exist in database. what can i do for it?
thanks

my drop down list:

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

Answer Source

You can do this during data binding:

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

Or you can add default markup as

<asp:DropDownList .. AppendDataBoundItems="true">
   <Items>
       <asp:ListItem Text="Select" Value="" />
   </Items>
</asp:DropDownList>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download