Reputation: 700
I have a databound dropdownlist on a page, where in the page_load I set the selectedValue (inside a 'not isPostBack').
Although the page displays fine and shows the correct item as selected.. inside the page_load if I try and get the selectedValue() and display it to the screen, I always get null... selectedIndex is -1.
I have a button, which when clicked refers to this ddl's selectedValue, and here it pulls through the the expected result.. so how come I can't see it immediately after setting it, while still in page_load?
cheers :)
edit: the code..
ddl declaration
<asp:DropDownList runat="server" ID="dlCountryList" DataSourceID="dsCountryList"
DataValueField="countrylistid" DataTextField="description">
</asp:DropDownList>
and the page_load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dlCountryList.SelectedValue = "GR"
Response.Write("*" + CStr(dlCountryList.SelectedIndex) + "*")
End Sub
Upvotes: 1
Views: 1523
Reputation: 66389
Manually call the DataBind() method of the drop down after setting the selected value.
The SelectedValue is not ordinary get/set property of the control, by setting it you only set some "flag" that is used when the drop down is binded.
By default it's binded after the Page_Load event (not sure which event exactly) but it's also possible to call it manually.
Upvotes: 1