2

I need to set value "Y" to dropdownlist control dynamically.When i tried by selectedValue it gave error like object reference is null .plz help

3 Answers 3

3

First make sure that Y is there inside your asp:DropDownList. Then do this

if (DropDownList1.Items.FindByValue("Y") != null)
{
     DropDownList1.Items.FindByValue("Y").Selected = true;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that "Y" is in your dropdownlist:

DropDownList1.SelectedValue=DropDownList1.Items.FindByValue("Y").Tostring();

It works.

Comments

0

SelectedValue will only work when using data binding. If you filled out the list manually via the Windows Forms Designer in Visual Studio or by manipulating the DropDownList.Items collection, you need to use SelectedItem as below:

DropDownList1.SelectedItem = "Y";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.