设置数据绑定 DropDownList 的 SelectedValue
我有一个 asp.net dropDownList,它在页面加载时自动绑定到 sqlDataSource 到客户端类型的值。在页面加载时,我还创建了一个 Client 对象,它的属性之一是 ClientType。我试图根据 Client 对象的 ClientType 属性的值设置 ddl 的 SelectedValue ,但未成功。我收到以下错误消息“System.ArgumentOutOfRangeException:'ddlClientType'具有无效的 SelectedValue,因为它不存在于项目列表中”。据我所知,这是因为当我尝试设置所选值时列表尚未填充。有办法克服这个问题吗?谢谢你!
I have an asp.net dropDownList which is automatically bound to a sqlDataSource to values of client type on page load. On page load I am also creating a Client object, one of it's properties is ClientType. I am trying to set the SelectedValue of the ddl according to the value of the ClientType property of the Client object unsuccessfully. I recieve the following error message "System.ArgumentOutOfRangeException: 'ddlClientType' has a SelectedValue which is invalid because it does not exist in the list of items". I understand that this is because the list has not yet been populated when I'm trying to set the selected value. Is there a way of overcoming this problem? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在设置选定值之前,检查项目是否在列表中,然后通过索引选择它
编辑:已添加...
如果您在页面加载中进行绑定操作,请尝试按照以下方式操作:
DataBind 中()
方法Before setting a selected value check whether item is in list and than select it by index
EDIT: Added...
If you are doing binding stuff in Page Load, try to follow this way:
DataBind()
method您必须使用 DataBound 事件,一旦数据绑定完成,它将被触发。
如果您确实想查看页面加载事件中的值,请在设置值之前调用
DataBind()
方法。 ..You have to use the DataBound Event, it will be fired, once databinding is complete
If you really want to see the value in the Page load event, then call the
DataBind()
method before setting the value...