[英]How to select a prepopulated dropdownlist option in a popup before showing from code-behind
This line sets the selected index to the correct number: 这行将所选索引设置为正确的数字:
ddlCliNewMsg.SelectedIndex = ddlCliNewMsg.Items.IndexOf(ddlCliNewMsg.Items.FindByValue(dr["ClientText"].ToString()));
But when the popup loads, the 0 index is selected. 但是,当加载弹出窗口时,将选择0索引。
Change 更改
if (dr["ClientText"].ToString().Length > 0)
{
ddlCliNewMsg.SelectedValue = dr["ClientText"].ToString();
}
To 至
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.