ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

SelectItem

Select a item in a DropDownList by value.

Source

/// <summary>
/// Select a item in a DropDownList by informed value.
/// </summary>
/// <param name="ddl">DropDownList object.</param>
/// <param name="value">Value to be selected.</param>
public static void SelectItem(this DropDownList ddl, string value)
{
	if (ddl.Items.FindByValue(value) != null)
		ddl.Items.FindByValue(value).Selected = true;
}

Example

cbo.SelectItem("1");

Author: Carlos Alessandro Ribeiro

Submitted on: 21 jul. 2009

Language: C#

Type: System.Web.UI.WebControls.DropDownList

Views: 5129