C# 下拉列表选择的值不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10429322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dropdown list selected value not working
提问by user998405
In my ASP.NET project. I have two dropdownlist and a checkbox. When the checkbox is checked, the selected value of DropDownList1must be same as selcted value of the DropDownList2. But the DropDownList1.SelectedValueis not working.
在我的 ASP.NET 项目中。我有两个下拉列表和一个复选框。选中该复选框时, 的选定值DropDownList1必须与的选定值相同DropDownList2。但DropDownList1.SelectedValue不起作用。
Here is my code:
这是我的代码:
protected void chkSameBAddress_CheckedChanged(object sender, EventArgs e)
{
try
{
if (this.chkSameBAddress.Checked == true)
{
this.txtcSAddress1.Text= this.txtcBAddress1.Text;
this.txtcSAddress2.Text = this.txtcBAddress2.Text;
this.txtcSAddress3.Text = this.txtcBAddress3.Text;
this.txtcSAddress4.Text = this.txtcBAddress4.Text;
this.txtcSCity.Text = this.txtcBCity.Text;
this.txtcSPostCode.Text = this.txtcBPostCode.Text;
this.txtcSState.Text = this.txtcBState.Text;
this.ddlcSCountry.Items.FindByValue(ddlcBCountry.SelectedItem.Value).Selected = true;
}
}
catch (Exception ex)
{
logger.Error(ex.Message);
throw;
}
}
As seen in the example above, if chkSmaeBAddressis checked then the selected value of ddlcSCountrymust be same as ddlcBCountryselected value.
如上面的例子中看到的那样,如果chkSmaeBAddress被选中,那么所选择的值ddlcSCountry必须为是相同ddlcBCountry选择的值。
采纳答案by Kaf
Where are you binding data to these dropdown list controls? They should be bound only in the initial loading of the page as follows. I suspect that you are binding them in every page load and therefore selected values disappear.
您在哪里将数据绑定到这些下拉列表控件?它们应该只在页面的初始加载中绑定,如下所示。我怀疑您在每个页面加载中都绑定了它们,因此选定的值会消失。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Please check if you are binding checkbox controls here.
//If not bring them in here
}
}
Other condition is that both ddlcSCountry and ddlcBCountry hould have same values to be able to select. Otherwise ddlcSCountry.Items.FindByValue(ddlcBCountry.SelectedItem.Value)will be null and will throw an error when trying to set the Selected property
其他条件是 ddlcSCountry 和 ddlcBCountry 都应该具有相同的值才能选择。否则ddlcSCountry.Items.FindByValue(ddlcBCountry.SelectedItem.Value)将为 null 并在尝试设置 Selected 属性时抛出错误
If both above conditions are okay, your code should work.
如果上述两个条件都可以,则您的代码应该可以工作。
EDITSorry, my commented code should be to check binding of dropdown list controls not the checkbox. so it should be as
编辑抱歉,我注释的代码应该是检查下拉列表控件的绑定而不是复选框。所以它应该是
//Please check if you are binding both dropdown list controls here.
//If not bind them within the if (!Page.IsPostBack)
Put a breakpoint in your if (this.chkSameBAddress.Checked == true)line within CheckedChanged eventand see it is executing and then the runtime values...
在您的if (this.chkSameBAddress.Checked == true)行中放置一个断点CheckedChanged event并查看它正在执行,然后运行时值...
回答by Likurg
Try this for select
试试这个选择
ddlcSCountry.Text=ddlcBCountry.SelectedItem.Value;
It will selected needed item
它将选择需要的项目
回答by npclaudiu
Make sure that chkSameBAddress.AutoPostBackis set to true. If it is set and still doesn't work, consider using an UpdatePanelcontrol or moving that logic to the client using JavaScript.
确保将chkSameBAddress.AutoPostBack其设置为 true。如果它已设置但仍然不起作用,请考虑使用UpdatePanel控件或使用 JavaScript 将该逻辑移至客户端。
回答by Doomsknight
Surely you are trying to make the dropdown boxes equal?
您确定要使下拉框相等吗?
use
用
ddlcSCountry.SelectedIndex = ddlcSCountry.FindStringExact(ddlcBCountry.Text);
This will select the matching option in the list and not just set the text in the field, which is very useful when you have underlying values with your text options.
这将选择列表中的匹配选项,而不仅仅是设置字段中的文本,当您的文本选项具有基础值时,这非常有用。
回答by bmich72
Make sure you have AutoPostBack set to true in the properties of the DropDownList.
确保在 DropDownList 的属性中将 AutoPostBack 设置为 true。
回答by Oliver Lundt
The accepted solution is an obvious solution to the most common cause, however, there is one more surprising issue that can cause this!
公认的解决方案是最常见原因的明显解决方案,但是,还有一个更令人惊讶的问题可能导致这种情况!
My list values came from a database and the values had linefeed and carriage return from the database values: \r\n. These values look like an innocent space, but actually they are not!
我的列表值来自数据库,并且这些值具有来自数据库值的换行和回车:\r\n. 这些值看起来像一个无辜的空间,但实际上它们不是!
My solution was to remove these hidden Char values. Hope it helps.
我的解决方案是删除这些隐藏的 Char 值。希望能帮助到你。
回答by Jeff George
I just switch to using <select runat="server" id="test1"></Select>I only had to make slight modifications the code behind and it all worked better.
我只是切换到使用<select runat="server" id="test1"></Select>我只需要稍微修改后面的代码,一切都变得更好了。

