English 中文(简体)
Why might dropdownlist.SelectedIndex = value fail?
原标题:

I have a dropdown list that I am binding to a datatable. Here is the code I am using to do it:

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

Unfortunately, the line ddlBuildAddr.SelectedIndex = addressId is failing. Looking at this line through the debugger, the SelectedIndex goes to -1, while addressId goes to 2. What gives? Why would the assignment operator flatout not work?

最佳回答

Move your ddlDeptName.DataBind() to before you try to set the selected index. Before you bind, you don t actually have any items in the dropdown so an index of 2 is invalid.

问题回答

Replace this line

ddlDeptName.SelectedIndex = addressId

With this:

ddlDeptName.SelectedValue = addressId.ToString()

As for why it s failing - addressId is likely out of the range of possible index values of your drop down list.

I think that you need to set the SelectedValue property instead.

as TLiebe has pointed out, move your databind to BEFORE you try to set the selected. the databinding method basically wipes out any previous state you have set on the combo-box.

second, the selected index is NOT the datavalue or the selected value members. it is the index of the ListItem in the dropdown s collection of items such that dropdown[ dropdown.selectedindex] will give you the item from the list that is marked as selected. so, you must find the item you want to select, then set the selectedindex to the index of that item.

a VB example from MSDN:

  Selects the item whose text is Apples
ListBox1.Items.FindByText("Apples")
If Not li Is Nothing Then
   li.Selected = True
End If

// Selects the item whose text is Apples
ListItem li = ListBox1.Items.FindByText("Apples");
if(li != null)
{
   li.Selected = true;
}

Use this

ddlBuildAddr.DataSource = buildings
ddlBuildAddr.DataTextField = "buildingName"
ddlBuildAddr.DataValueField = "buildingId"
Dim addressId As Int32 = OfficeData.GetInstance().GetBuildingId(currentAddress)

ddlBuildAddr.Databind()

After

foreach (var item in ddlBuildAddr.Items)
{
  if(Convert.toInt32(item.value)==addressId)
  {
     item.selected=true;
     break;
  }
}

Change your code as follows....

ddlBuildAddr.DataSource = buildings
ddlBuildAddr.DataTextField = "buildingName"
ddlBuildAddr.DataValueField = "buildingId"
ddlBuildAddr.DataBind()

Dim addressId As Int32 = OfficeData.GetInstance().GetBuildingId(currentAddress)

ddlBuildAddr.SelectedValue = addressId //It may throw error if item is not found in the list
(or)
ddlBuildAddr.Items.FindByValue(addressId).Selected = true;
(or)
ListItem lstNew = ddlBuildAddr.Items.FindByValue(addressId)
ddlBuildAddr.selectedItem = lstNew




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签