为什么dropdownlist.SelectedIndex =值失败?

1 vb.net asp.net drop-down-menu

我有一个下拉列表,我绑定到数据表.这是我用来做的代码:

ddlBuildAddr.DataSource = buildings
ddlBuildAddr.DataTextField = "buildingName"
ddlBuildAddr.DataValueField = "buildingId"
Dim addressId As Int32 = OfficeData.GetInstance().GetBuildingId(currentAddress)
ddlBuildAddr.SelectedIndex = addressId
ddlBuildAddr.DataBind()
Run Code Online (Sandbox Code Playgroud)

不幸的是,这条线路ddlBuildAddr.SelectedIndex = addressId失败了.通过调试器查看这一行,SelectedIndex转到-1,同时addressId转到2.什么给出?为什么赋值运算符flatout不起作用?

TLi*_*ebe 5

在尝试设置所选索引之前,将ddlDeptName.DataBind()移动到.在绑定之前,下拉列表中实际上没有任何项目,因此索引2无效.