Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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
First make sure that Y is there inside your asp:DropDownList. Then do this
asp:DropDownList
if (DropDownList1.Items.FindByValue("Y") != null) { DropDownList1.Items.FindByValue("Y").Selected = true; }
Add a comment
Assuming that "Y" is in your dropdownlist:
DropDownList1.SelectedValue=DropDownList1.Items.FindByValue("Y").Tostring();
It works.
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:
SelectedValue
DropDownList.Items
SelectedItem
DropDownList1.SelectedItem = "Y";
Required, but never shown
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.
Explore related questions
See similar questions with these tags.