Thursday, September 1, 2011

Select Drop Down List Item After Declarative Databinding

The new declarative controls of ASP.NET 2.0 are really great. However, there are some times, when it may not seem so.
Let's say you have a DropDownList which is bound by a DataSource control. Also, you have other pages hitting this page, with querystrings, and based on the item in the querystring, you'd like the to select that item.
In ASP.NET 1.1, you'd just code it, and select it after the code, or in the Pre-render event.
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(Request.Querystring("YourQuerystring"));

Source: https://www.nilebits.com/blog/2010/07/select-dropdownlist-item-after-declarative-databinding/