Tech Junkie Blog - Real World Tutorials, Happy Coding!: ASP.NET DropDownList Part 2: Setting Default Value on First Page Load

Wednesday, July 16, 2014

ASP.NET DropDownList Part 2: Setting Default Value on First Page Load

In part one DropDownList Part 1: Bind a DataTable to A DropDownList we learned how to bind a DataTable to a DropDownList.  In this tutorial we will learn to set the default value of the DropDownList control on the page's first load that is before there is a post back to the page.  This behavior is commonly used when you want to default the drop down list to a particular selection.  Such as the "United States" in a list of countries drop down list.

To set a default value in the DropDownList control follow the steps below.

1. Add the SetDefaultSelectItem method to the code behind file.
protected void SetDefaultSelectItem(string txtSelect)
   DropDownList1.Items.FindByText(txtSelect).Selected = true;

2. Add a condition to only set the default value on the first page load
protected void Page_Load(object sender, EventArgs e)
3. If you run the application you will now see that "Seafood" is the default selection on the DropDownList control, when the page first load
Default DropDownList selection

Related Blogs:

  1. Bind a DataTable To A DropDownList
  2. (DropDownList) Setting Default Value on First Page Load
  3. Use The DropDownList Control To Populate GridView
  4. Bind DropDownList To A List Of Objects

No comments:

Post a Comment

Search This Blog