Saturday, March 2, 2013

Selecting value in dropdownlist:


Selecting value in dropdownlist:

   Dropdownlist.SelectedItem.Text = “two”;

To Select value in dropdownlist don’t use the above code snippet it will overwrite the existing item.

This is not good coding practice. Instead use Inbuilt function following

Consider dropdown binded by following value field and text field.

Valuefield          Textfield
    1                       One
    2                       two   
    3                       three
    4                       four

If u want “two” as selected text following is the way.

(i) Using textfield :

If(DropDownList.Items.FindByValue("two") != null)
{
      Dropdownlist.Items.FindByText (“two”).selected = true.
}

ii) Using valuefield:

If(DropDownList.Items.FindByValue("two") != null)
{
      Dropdownlist. Items.FindByValue (“2”).selected = true.
}