Reputation: 510
I have a DropDownList
control in my ASP.NET C# web application that reads from SqlDataSource
. I have a list of items that are displayed, but I want to add more text to the first item to say "Please select department" with value == 0
.
Here's my code:
<asp:DropDownList
ID="DropDownList1"
DataSourceID="sdsdepartment"
runat="server"
DataTextField="department_name"
DataValueField="deptid"
></asp:DropDownList>
<asp:SqlDataSource
ID="sdsdepartment"
runat="server"
ConnectionString="<%$ ConnectionStrings:blabla %>"
SelectCommand="SELECT [department_name], [deptid] FROM [DEPARTMENT]"
></asp:SqlDataSource>
Upvotes: 3
Views: 73
Reputation: 379
This will be very simple
<asp:DropDownList
ID="DropDownList1"
DataSourceID="sdsdepartment"
runat="server"
DataTextField="department_name"
DataValueField="deptid"
AppendDataBoundItems="True" // set this property to True
>
<asp:ListItem Text="select Department" Value="0"></asp:ListItem> //here add one default item
</asp:DropDownList>
<asp:SqlDataSource
ID="sdsdepartment"
runat="server"
ConnectionString="<%$ ConnectionStrings:blabla %>"SelectCommand="SELECT [department_name],
[deptid] FROM [DEPARTMENT]"
></asp:SqlDataSource>
Upvotes: 0
Reputation: 11
in the Page_Load add the following line.
DropDownList1.Items.Insert(0, new ListItem("Please select department", "0"));
Upvotes: 0
Reputation: 7488
You can change your SelectCommand to:
SELECT 'Please select department' as [department_name], 0 as [deptid]
UNION
SELECT [department_name], [deptid] FROM [DEPARTMENT]
Upvotes: 2