dropdownlist.Text 不工作
dropdownlist.Text not working
下拉列表始终显示从数据库填充的项目的第一个索引,并且在调试模式下ddlcountry.Text
始终为空字符串("")
。
我的下拉列表中有 "Philippines" 项,但 "Argentina" 总是首先显示在我的下拉列表中,而不是 "Philippines"。
请帮忙。
//in formload
if(!isPostback)
{
DataTable dtCountry= new DataTable();
dtCountry= network.GetCountry();
for (int row = 0; row < dtCountry.Rows.Count; row++)
{
ddlCoutry.Items.Add(new ListItem { Text = dtCountry.Rows[row][1].ToString(), Value = dtCountry.Rows[row][1].ToString() });
}
}
ddlCountry.Text = "Philippines";
将所选项目设置为 "Philippines",因为我假设您的国家/地区列表是按字母顺序排列的。
ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByText("Philippines"));
另外我想指出你的变量拼写错误:
**ddlCoutry**.Items.Add(new ListItem { Text = dtCountry.Rows[row][1].ToString(), Value = dtCountry.Rows[row][1].ToString() });
正如我在上面的评论中提到的,我认为你的问题是你试图通过文本 select 下拉选项,但与 .Text
属性 混淆了。你可以这样做:-
ddlCountries.Items.FindByText("Philippines").Selected = true;
下拉列表始终显示从数据库填充的项目的第一个索引,并且在调试模式下ddlcountry.Text
始终为空字符串("")
。
我的下拉列表中有 "Philippines" 项,但 "Argentina" 总是首先显示在我的下拉列表中,而不是 "Philippines"。
请帮忙。
//in formload
if(!isPostback)
{
DataTable dtCountry= new DataTable();
dtCountry= network.GetCountry();
for (int row = 0; row < dtCountry.Rows.Count; row++)
{
ddlCoutry.Items.Add(new ListItem { Text = dtCountry.Rows[row][1].ToString(), Value = dtCountry.Rows[row][1].ToString() });
}
}
ddlCountry.Text = "Philippines";
将所选项目设置为 "Philippines",因为我假设您的国家/地区列表是按字母顺序排列的。
ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByText("Philippines"));
另外我想指出你的变量拼写错误:
**ddlCoutry**.Items.Add(new ListItem { Text = dtCountry.Rows[row][1].ToString(), Value = dtCountry.Rows[row][1].ToString() });
正如我在上面的评论中提到的,我认为你的问题是你试图通过文本 select 下拉选项,但与 .Text
属性 混淆了。你可以这样做:-
ddlCountries.Items.FindByText("Philippines").Selected = true;