Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the default value to the drop down list control?

I have a drop down list control on my web page. I have bind the datatable to the dropdownlist control as follows -

lstDepartment.DataTextField = "DepartmentName";
    lstDepartment.DataValueField = "DepartmentID";
    lstDepartment.DataSource = dtDept;
    lstDepartment.DataBind();

in the page load event i want to set the default value to the drop down list control from my other table field.

how to do this?

like image 570
Priyanka Avatar asked Nov 01 '11 12:11

Priyanka


People also ask

How do I set default drop down value?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

How do you set a pre populated default value of a dropdown in Editscreen for a Sharepoint choice field?

- Add a new item, most of the TextInput will be BLANK (but not default value such as createddate, Auto-ID,..), so it will detect that this is a NEW record and will set the Dropdown1. Default = "Inland".

How do I set default value in person or group field?

So, we can easily set the default value to people picker control in PowerApps Forms, and we can update person or group field using Patch function. You just must generate the object with specific format which includes Claims, Email, Department, DisplayName, JobTitle and Picture fields.


2 Answers

After your DataBind():

lstDepartment.SelectedIndex = 0;  //first item

or

lstDepartment.SelectedValue = "Yourvalue"

or 
//add error checking, just an example, FindByValue may return null
lstDepartment.Items.FindByValue("Yourvalue").Selected = true;

or
//add error checking, just an example, FindByText may return null
lstDepartment.Items.FindByText("Yourvalue").Selected = true;
like image 168
rick schott Avatar answered Oct 18 '22 21:10

rick schott


if you know the index of the item of default value,just

lstDepartment.SelectedIndex = 1;//the second item

or if you know the value you want to set, just

lstDepartment.SelectedValue = "the value you want to set";
like image 31
ojlovecd Avatar answered Oct 18 '22 23:10

ojlovecd