如何检查下拉列表是否包含从数据库保存的值?

时间:2016-09-13 17:48:00

标签: c# asp.net sql-server database drop-down-menu

我有3个下拉菜单:财产,资本,企业。每个都有不同的价值观。 我正在保存用户在填写隐藏字段(hfFromEntity)中的表单时所做的选择。我在检索重新加载表单的信息时遇到问题。我想将隐藏字段的值与所有3个下拉列表进行比较,并选择具有该值的值。但不知道该怎么做。 在这里我如何填充下拉列表。

     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()));
            }
    }

2 个答案:

答案 0 :(得分:1)

您可以使用以下方法搜索下拉列表中显示的文本:

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

要按价值搜索,您需要使用:

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

答案 1 :(得分:0)

这会在组合框中找到代表你的字符串的项目,并为你提供你可以选择的项目:

var item = drpFromProperty.Items.FindByValue(hfhfWFromEntity.Value)
if (item != null)
    item.Selected = true;