我有一个按字母顺序排列的国家名称的下拉列表。我希望下拉菜单始终将(显示)印度作为默认值。我不想将选定的索引设置为常量,因为以后可能会将其他国家/地区添加到列表中。如何将索引设置为“印度”?
ddlCountryCode.DataSource = ds1.Tables["AUser"];
ddlCountryCode.DataTextField = "CountryCode";
ddlCountryCode.SelectedIndex =
ddlCountryCode.Items.IndexOf(ddlCountryCode.Items.FindByText("India(+91)"));
ddlCountryCode.DataBind();
不工作...
最佳答案
你可以这样使用
DropdownList1.SelectedIndex =
DropdownList1.Items.IndexOf(DropdownList1.Items.FindByValue(strText));
或
DropdownList1.SelectedIndex =
DropdownList1.Items.IndexOf(DropdownList1.Items.FindByText(strText));
引用文献
Setting dropdownlist selecteditem programmatically
编辑1
更改代码的顺序
ddlCountryCode.DataSource = ds1.Tables["AUser"];
ddlCountryCode.DataTextField = "CountryCode";
ddlCountryCode.DataBind();
ddlCountryCode.SelectedIndex =
ddlCountryCode.Items.IndexOf(ddlCountryCode.Items.FindByText("India(+91)"));
关于c# - 设置下拉列表选择索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15539841/