在我的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选择的值相同.
解决方法@H_502_10@
您将哪些数据绑定到这些下拉列表控件?它们只能在初始加载页面时绑定如下.我怀疑你在每个页面加载时都绑定它们,因此选择的值消失.
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)行中放置一个断点,并看到它正在执行,然后运行时值…
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)行中放置一个断点,并看到它正在执行,然后运行时值…