简体   繁体   English

下拉列表选择的值不起作用

[英]Dropdown list selected value not working

In my ASP.NET project. 在我的ASP.NET项目中。 I have two dropdownlist and a checkbox. 我有两个下拉列表和一个复选框。 When the checkbox is checked, the selected value of DropDownList1 must be same as selcted value of the DropDownList2 . 当复选框被选中,所选择的值DropDownList1必须相同的selcted值DropDownList2 But the DropDownList1.SelectedValue is not working. 但是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 chkSmaeBAddress is checked then the selected value of ddlcSCountry must be same as ddlcBCountry selected value. 如上例所示,如果选中chkSmaeBAddress ,则所选的ddlcSCountry值必须与ddlcBCountry选定值相同。

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. 其他条件是ddlcSCountry和ddlcBCountry都应该具有相同的值才能选择。 Otherwise ddlcSCountry.Items.FindByValue(ddlcBCountry.SelectedItem.Value) will be null and will throw an error when trying to set the Selected property 否则ddlcSCountry.Items.FindByValue(ddlcBCountry.SelectedItem.Value)将为null并在尝试设置Selected属性时将引发错误

If both above conditions are okay, your code should work. 如果以上两个条件都可以,那么您的代码应该可行。

EDIT Sorry, 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 event and see it is executing and then the runtime values... CheckedChanged event中的if (this.chkSameBAddress.Checked == true)行中放置一个断点,看看它正在执行,然后是运行if (this.chkSameBAddress.Checked == true) ......

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. 这将选择列表中的匹配选项,而不仅仅是在字段中设置文本,这在您的文本选项具有基础值时非常有用。

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 . 我的列表值来自数据库,并且值已从数据库值换行换行和回车: \\r\\n These values look like an innocent space, but actually they are not! 这些价值看起来像一个无辜的空间,但实际上它们不是!

My solution was to remove these hidden Char values. 我的解决方案是删除这些隐藏的Char值。 Hope it helps. 希望能帮助到你。

Try this for select 试试这个选择

ddlcSCountry.Text=ddlcBCountry.SelectedItem.Value;

It will selected needed item 它将选择所需的项目

Make sure that chkSameBAddress.AutoPostBack is set to true. 确保chkSameBAddress.AutoPostBack设置为true。 If it is set and still doesn't work, consider using an UpdatePanel control or moving that logic to the client using JavaScript. 如果已设置但仍然不起作用,请考虑使用UpdatePanel控件或使用JavaScript将该逻辑移动到客户端。

确保在DropDownList的属性中将AutoPostBack设置为true。

我只是切换到使用<select runat="server" id="test1"></Select>我只需稍微修改后面的代码就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM