[英]How to get the new SelectedValue from a drop down list?
I have a SelectedIndexChanged event on my dropdownlist. 我的下拉列表中有一个SelectedIndexChanged事件。 Below you can see I'm trying to set a variable as the SelectedValue.
在下面,您可以看到我正在尝试将变量设置为SelectedValue。 But it's giving me the SelectedValue of the old selected item rather than the new one.
但这给了我旧选择项而不是新选择项的SelectedValue。 How do I acheive getting the new one?
如何获得新的?
protected void ddlAwesome_SelectedIndexChange(object sender, EventArgs e)
{
int ID = Convert.ToInt32(ddlAwesome.SelectedValue);
}
Thanks 谢谢
I think the problem is that it gives you the default value or the first one. 我认为问题在于它为您提供了默认值或第一个。
You must read this article : http://www.codeproject.com/Articles/776840/Why-DropDownList-SelectedValue-Does-Not-Work-Insid . 您必须阅读以下文章: http : //www.codeproject.com/Articles/776840/Why-DropDownList-SelectedValue-Does-Not-Work-Insid 。 The main problem is that you need a binding.
主要问题是您需要绑定。
private void BindDropDownList()
{
// Declare a Dictionary to hold all the Options with Value and Text.
Dictionary<string, string> options = new Dictionary<string, string>();
options.Add("-1", "Select Option");
options.Add("1", "Option 1");
options.Add("2", "Option 2");
options.Add("3", "Option 3");
options.Add("4", "Option 4");
options.Add("5", "Option 5");
// Bind the Dictionary to the DropDownList.
ddlDropDownId.DataSource = options;
ddlDropDownId.DataTextField = "value";
ddlDropDownId.DataValueField = "key";
ddlDropDownId.DataBind();
}
And on page load you must bind. 并且在页面加载时必须绑定。
if (!IsPostBack)
{
BindDropDownList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.