Pages

Showing posts with label dropdownlist. Show all posts
Showing posts with label dropdownlist. Show all posts

Friday, February 28, 2014

HOW TO: Add a default ListItem to a DropDownList

This post shows you how to add as many ListItems to a DropDownList before having DB databind on the same DropDownList .. It can be done one of 2 ways. A default ListItem can be added to a DropDownList programmatically with the following syntax after binding data to the DropDownList:
 
//Code here to populate DropDownList
DropDownListID.Items.Insert(0, new ListItem("Default text", "Default value")
This will add a ListItem to index 0, which will be the first ListItem.
In .NET 2.0, this can be done declaratively using the AppendDataBoundItems property. This will append all data-bound ListItems to the DropDownList, leaving those you add manually as the first selections.
<asp:DropDownList ID="DropDownListID" AppendDataBoundItems="true" runat="server">
     <asp:ListItem Text="Default text" Value="Default value" />
</asp:DropDownList>

Setting AppendDataBoundItems=True Property ensures that your datasource items will be appended without removing the initial item.

Saturday, November 30, 2013

How to bind LINQ data to dropdownlist (TEXT,VALUE)


Let's assume we have a Web Service which will return the results as an
object with the values DDLTEXT
and
DDLVALUE.
The following will explain how to use LINQ to query and then bind the results to the

DropDownList:
  DataTextField and  DataValueField

ddl.DataSource = wsMethod.Contents.OrderBy(o=>o.DDLTEXT);

//specify result object values
ddl.DataTextField = "DDLTEXT";
ddl.DataValueField = "DDLVALUE";

ddl.DataBind();