设置值后获取dropDownList的selectedValue()
我在页面上有一个数据绑定下拉列表,在 page_load 中我设置了 selectedValue (在“not isPostBack”内)。
尽管页面显示良好并显示了正确的项目。如果我尝试获取 selectedValue() 并将其显示到屏幕上,则在 page_load 内显示正确的项目,但我总是得到 null... selectedIndex 为 -1。
我有一个按钮,单击该按钮时会引用此 ddl 的 selectedValue,在这里它会提取预期的结果..那么为什么我在设置它后仍处于 page_load 状态时无法立即看到它?
干杯:)
编辑:代码..ddl
声明
<asp:DropDownList runat="server" ID="dlCountryList" DataSourceID="dsCountryList"
DataValueField="countrylistid" DataTextField="description">
</asp:DropDownList>
和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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置所选值后手动调用下拉列表的 DataBind() 方法。
SelectedValue 不是控件的普通获取/设置属性,通过设置它,您只需设置一些在绑定下拉列表时使用的“标志”。
默认情况下,它在 Page_Load 事件之后绑定(不确定到底是哪个事件),但也可以手动调用它。
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.