c# – 下拉列表选择的值不工作

在我的ASP.NET项目中我有两个下拉列表和一个复选框.选中复选框时,DropDownList1的选定值必须与DropDownList2的selcted值相同.但是DropDownList1.SelectedValue不起作用.

这是我的代码

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;

    }
}

如上面的示例所示,如果选中chkSmaeBAddress,则所选的ddlcSCountry值必须与ddlcBCountry选择的值相同.

解决方法

您将哪些数据绑定到这些下拉列表控件?它们只能在初始加载页面时绑定如下.我怀疑你在每个页面加载时都绑定它们,因此选择的值消失.
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
    }
}

其他条件是,ddlcSCountry和ddlcBCountry都具有相同的值才能选择.否则ddlcSCountry.Items.FindByValue(ddlcBCountry.SelectedItem.Value)将为空,并在尝试设置Selected属性时将抛出错误

如果上述两种情况都可以,您的代码应该可以工作.

编辑对不起,我的评论代码应该是检查绑定下拉列表控件不是复选框.所以应该是

//Please check if you are binding both dropdown list controls here. 
//If not bind them within the if (!Page.IsPostBack)

在CheckedChanged事件中的if(this.chkSameBAddress.Checked == true)行中放置一个断点,并看到它正在执行,然后运行时值…

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...