Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a drop down list which gets its data from a database.

I want to add a label "select" which is not part of the data in the database.


Thanks.

What I have tried:

protected void Page_Load(object sender, EventArgs e)
        {       
          DDTitle.Items.Insert(0, new ListItem("Select Title", "0"));
          DDTitle.SelectedIndex = 0;



}

}
Posted
Updated 19-Apr-17 9:24am

try like this

protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {

              DataTable dt = new DataTable();
              dt.Columns.Add("ValueColumn");
              dt.Columns.Add("TextColumn");
              dt.Rows.Add(1, "one");
              dt.Rows.Add(2, "two");
              dt.Rows.Add(3, "three");

              DDTitle.DataValueField = "ValueColumn";
              DDTitle.DataTextField = "TextColumn";
              DDTitle.DataSource = dt;
              DDTitle.DataBind();
              DDTitle.Items.Insert(0, new ListItem("Select Title", "0")); // this line should at the last
          }

      }


remove the hard coding and get the data from database.
 
Share this answer
 
v3
<asp:DropDownList ID="DDTitle" runat="server"    DataTextField="Title" AutoPostBack="True" DataSourceID="SqlTitle" AppendDataBoundItems="true"></asp:DropDownList></td>

I had to set the AppendDataBoundItems=true.

thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900