我有一个asp.net dropDownList它自动绑定到一个sqlDataSource的客户端类型的值在页面加载。在页面加载时,我也创建了一个Client对象,其中一个属性是ClientType。我试图根据Client对象的ClientType属性的值设置ddl的SelectedValue失败。我收到以下错误消息“System.ArgumentOutOfRangeException:'ddlClientType'有一个无效的SelectedValue,因为它不存在于项目列表中”。我知道这是因为当我尝试设置选定的值时,列表尚未填充。有没有办法克服这个问题?谢谢!设置数据绑定的SelectedValue DropDownList
5
A
回答
5
您必须使用数据绑定事件,它会被解雇,一旦绑定完成
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
// You need to set the Selected value here...
}
如果你真的想看到在页面加载事件的值,然后设置前致电DataBind()
方法值...
protected void Page_Load(object sender, EventArgs e)
{
DropdownList1.DataBind();
DropdownList1.SelectedValue = "Value";
}
+0
我试过了,仍然收到相同的错误信息。 –
+0
检查我的编辑部分。 –
+0
我再次尝试使用dataBound事件,我不再收到错误消息,但未选择该值。 –
4
之前设置所选值检查项目是否在列表中按索引选择它
<asp:DropDownList id="dropDownList"
AutoPostBack="True"
OnDataBound="OnListDataBound"
runat="server />
protected void OnListDataBound(object sender, EventArgs e)
{
int itemIndex = dropDownList.Items.IndexOf(itemToSelect);
if (itemIndex >= 0)
{
dropDownList.SelectedItemIndex = itemIndex;
}
}
编辑:添加...
如果在页面加载做绑定的东西,尝试按照这种方式:
- 移动在被覆盖的
DataBind()
方法 - 所有绑定相关的代码在Page_Load中加入:(在控件不直接调用DataBind的情况下,这是父页的责任)
if (!IsPostBack)
{
Page.DataBind(); // only for pages
}
相关问题
- 1. DropDownList设置错误SelectedValue
- 2. Dropdownlist重置SelectedValue
- 3. 为一个DropDownList设置SelectedValue也更新SelectedValue另一个DropDownLIst
- 4. 无法设置的SelectedValue为DropDownList的
- 5. 在UpdatePanel中设置DropDownList的SelectedValue
- 6. 在DataGrid中设置DropDownList的SelectedValue
- 7. 在设置值后获取dropDownList的selectedValue()
- 8. 在PageLoad上设置DropDownList的SelectedValue
- 9. 将DropDownList SelectedValue设置为一个有时为空值的绑定字段
- 10. dropdownlist selectedvalue
- 11. Dropdownlist数据绑定自动设置选定值?
- 12. 设置GridDropDownColumn的SelectedValue
- 13. 数据绑定组合框的SelectedValue
- 14. Asp.Net MVC DropDownList数据绑定
- 15. DropDownList selectedvalue and tables
- 16. 实时DropdownList SelectedValue
- 17. DropDownList的SelectedValue与SelectedItem.Value
- 18. 绑定的DropDownList的SelectedValue到DataValueField为GridView控件更新
- 19. 如何在绑定之前更改DropDownList的SelectedValue?
- 20. 。设置数据绑定
- 21. dropdownlist自定义数据绑定
- 22. 如何在GridView的EditTemplate中设置DropDownList的SelectedValue
- 23. datagrid editcommand set dropdownlist selectedvalue
- 24. 数据绑定DropDownList的DataGrid中 - 结合
- 25. 添加项数据绑定的DropDownList
- 26. 将数据绑定到ASP.NET中的DropDownList
- 27. 在转发器中设置Dropdownlist的Selectedindex/selectedvalue不起作用
- 28. 的DropDownList的SelectedValue不工作
- 29. 如何在ASP.NET数据绑定DropDownList中将TimeZoneInfo设置为用户的时区?
- 30. 将DropDownList数据源绑定到XPathSelect
你能发表一些代码吗? –