How to programmatically set selected value of Dropdownlist aspx during loading of data:
We can select programmatically a value on dropdownlist by it’s index.
If we need to select by text then we need FindByText method as described in following line.
DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(“your default selected text”));
Complete example is described in following code, it’s very simple and easy.
[sociallocker]
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { LoadUserDropDownList(); } } private void LoadUserDropDownList() { DropDownList1.DataTextField = "user_name"; DropDownList1.DataValueField = "user_id"; DropDownList1.DataSource = getUserData();// get the data DropDownList1.DataBind(); // Default selected value DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("your default selected text")); }
[/sociallocker]
Need your motivated comments to continue.