I am a new ASP.NET developer and I am trying to learn Linq-To-Entities. I am trying to associate a DropDownList with a Linq statement to get a status list in a status object. Everything is working fine. However, I am currently trying to add the “Select” option to DropDownList, but it doesn’t work with me. Could you tell me how to fix this?
ASP.NET Code:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
Code-Behind:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList1.Items.Add(new ListItem("Select", "0", true)); bindStatusDropDownList(); } } private void bindStatusDropDownList() { Status status = new Status(); DropDownList1.DataSource = status.getData(); DropDownList1.DataValueField = "ID"; DropDownList1.DataTextField = "Description"; DropDownList1.DataBind(); }
UPDATE:
I also tried to make DropDownList in the markup set, but it did not work with me.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem Selected="True" Value="0" Text="Select"></asp:ListItem> </asp:DropDownList>
user3107976 Dec 20 '13 at 9:37 2013-12-20 09:37
source share