为什么我的DropDownList的SelectedItem每次仅显示列表中的第一项? [英] Why is my DropDownList's SelectedItem only showing the first item in the list each time?
本文介绍了为什么我的DropDownList的SelectedItem每次仅显示列表中的第一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用XML文件填充的ASP.NET DropDownList
时遇到问题:
I am having a problem with an ASP.NET DropDownList
which is populated by an XML file:
rblState.DataSource = dsState;
rblState.DataValueField = "abbreviation";
rblState.DataTextField = "name";
rblState.DataBind();
这可以正常工作并显示所有正确的数据,但是,当我尝试检索选定的数据时会出现问题单击按钮后,列表中的值:
This works fine and displays all the right data however, the problem occurs when I try and retrieve the selected value from the list after a button has been clicked:
string state = rblState.SelectedItem.Text;
Console.WriteLine(state);
这总是只输出列表中的第一个值。
This always outputs only the first value within the list.
有人知道解决方案吗?
推荐答案
您可能正在重新绑定 DataBack
。而是这样做:
You are probably re-binding the DataSource
on PostBack. Instead, do this:
//only bind on the first request
if (!Page.IsPostBack)
{
rblState.DataSource = dsState;
rblState.DataValueField = "abbreviation";
rblState.DataTextField = "name";
rblState.DataBind();
}
这篇关于为什么我的DropDownList的SelectedItem每次仅显示列表中的第一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文