微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)行中放置一个断点,并看到它正在执行,然后运行时值…

原文地址:https://www.jb51.cc/csharp/93799.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐