问题描述:

I have 3 dropdowns: Property, capital, corporate. each has a different values.

I am saving the selection that users make when filling the form inside a hidden field (hfFromEntity). I have problem retrieving the information for reloading the form. I want to compare the value of the hidden field with all 3 dropdowns and select the one that has the value in it. But don't know how to do it.

Here how I populate the dropdowns.

 private void PopulateDropDownValues()

{

DataTable dt = objMrsManager.GetAllProperties();

DataTable dc = objMrsManager.GetlAllCapitals();

DataTable ds = objMrsManager.GetlAllCorporates();

if (dt != null && dt.Rows.Count > 0)

{

foreach (DataRow dr in dt.Rows)

{

drpFromProperty.Items.Add(new ListItem(dr["NAME"].ToString(), dr["ENTITYID"].ToString()));

drpToProperty.Items.Add(new ListItem(dr["NAME"].ToString(), dr["ENTITYID"].ToString()));

}

}

if (dc != null && dc.Rows.Count > 0)

{

foreach (DataRow dr in dc.Rows)

{

drpFromCapital.Items.Add(new ListItem(dr["NAME"].ToString(), dr["ENTITYID"].ToString()));

drpToCapital.Items.Add(new ListItem(dr["NAME"].ToString(), dr["ENTITYID"].ToString()));

}

}

if (ds != null && ds.Rows.Count > 0)

{

foreach (DataRow dr in ds.Rows)

{

drpFromCorporate.Items.Add(new ListItem(dr["NAME"].ToString(), dr["ENTITYID"].ToString()));

drpToCorporate.Items.Add(new ListItem(dr["NAME"].ToString(), dr["ENTITYID"].ToString()));

}

}

drpFromCorporate.Items.Insert(0, new ListItem("--Select Corporate--", "0"));

drpToCorporate.Items.Insert(0, new ListItem("--Select Corporate--", "0"));

drpFromCapital.Items.Insert(0, new ListItem("--Select Capital--", "0"));

}

private void LoadWireTransfers()

{

if (WireTransfers != null && WireTransfers.Rows.Count > 0)

{

DataRow dr = WireTransfers.Rows[CurrentPosition];

hfWireTransferID.Value = dr["WTID"].ToString();

hfWFromEntity.Value = dr["WFromEntity"].ToString();

hfWToEntity.Value = dr["WToEntity"].ToString();

//I want to compare the hfWFromEntityValue with each dropdownlists

if (hfhfWFromEntity.Value == drpFromProperty.????)

{

drpFromProperty.Items.Clear();

drpFromProperty.Items.Add(new ListItem(dr["WFromEntity"].ToString()));

}

//This doesn't work. I don't know how else I can do

if (drpFromCapital.Items.Contains(new ListItem(hfWFromEntity.Value)))

{

drpFromCapital.Items.Clear();

drpFromCapital.Items.Add(new ListItem(dr["WFromEntity"].ToString()));

}

if (hfhfWFromEntity.Value == drpFromCorporate.????)

{

drpFromCorporate.Items.Clear();

drpFromCorporate.Items.Add(new ListItem(dr["WFromEntity"].ToString()));

}

}

网友答案:

This finds the item in the combobox that represents your string, and gives you the item you can select:

var item = drpFromProperty.Items.FindByValue(hfhfWFromEntity.Value)
if (item != null)
    item.Selected = true;
网友答案:

You can search for the text that shown in the dropdown list by using:

    if (drpFromCapital.Items.FindByText(hfWFromEntity.Value) != null)
    {
        drpFromCapital.Items.Clear();
        drpFromCapital.Items.Add(new ListItem(dr["WFromEntity"].ToString()));
    }

To get search by value you need to use:

if (drpFromCapital.Items.FindByValue(hfWFromEntity.Value) != null)
{
    drpFromCapital.Items.Clear();
    drpFromCapital.Items.Add(new ListItem(dr["WFromEntity"].ToString()));
}
相关阅读:
Top