如何检查下拉列表是否包含从数据库保存的值
本文关键字:包含 数据库 保存 是否 下拉列表 何检查 检查 | 更新日期: 2023-09-27 18:06:55
我有3个下拉列表:Property, capital, corporate。每个都有不同的值。我保存用户在隐藏字段(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()));
}
}
可以使用以下命令搜索下拉列表中显示的文本:
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()));
}
查找代表字符串的组合框中的项,并给出您可以选择的项:
var item = drpFromProperty.Items.FindByValue(hfhfWFromEntity.Value)
if (item != null)
item.Selected = true;