如何在从代码后面显示之前,在弹出窗口中选择预先填充的下拉列表选项

How to select a prepopulated dropdownlist option in a popup before showing from code-behind

本文关键字:选择 窗口 填充 选项 下拉列表 代码 显示      更新时间:2023-09-26

此行将所选索引设置为正确的数字:

    ddlCliNewMsg.SelectedIndex = ddlCliNewMsg.Items.IndexOf(ddlCliNewMsg.Items.FindByValue(dr["ClientText"].ToString()));

但是当弹出窗口加载时,0索引被选中。

更改

    if (dr["ClientText"].ToString().Length > 0)
    {
    ddlCliNewMsg.SelectedValue = dr["ClientText"].ToString();
    }

    if (dr["ClientText"].ToString().Length > 0)
    {
    ddlCliNewMsg.ClearSelection(); //making sure the previous selection has been cleared
    ddlCliNewMsg.Items.FindByValue(dr["ClientText"].ToString()).Selected = true;
    }
    for populating dropdownlist use below code:
ddlCliNewMsg.DataSource = dataSource;
ddlCliNewMsg.DataTextField = textField;
ddlCliNewMsg.DataValueField = valueField;
ddlCliNewMsg.DataBind();
    textField and valueField is data field for text and value of dropdownlist.
    for selecting item with your value use below code:
 ddlCliNewMsg.Items.FindByValue("your value").Selected = true;
    replace your value with "your value"