LookupEdit.EditValueChanged事件内给其它文本框赋值无效或变回原来的值


在开发应用中,通常当选择一个值时要自动带出相关数据(给其它输入框组件赋值),比如一个选择客户的LookupEdit组件,取名为txtCustomer,选择客户的同时要自动带出客户的名称、联系人以及联系电话在对应的输入框中显示出来。我们可以在EditValueChanged、Validating、或者TextChanged事件内处理。
有一种异常情况
假设在EditValueChanged事件内处理上述需求,当选择客户后能自动带出联系人及电话号码,但是客户选择框本身被莫名其妙清空了或者选择无效(变回原来的值)!
原因:若在EditValueChanged事件内给其他绑定数据源的组件赋值时,会重置当前组件的EditValue的值。
解决方案
必须在EditValueChanged事件内第一行代码调用SetEditorBindingValue(txtCustomer,txtCustomer.EditValue),但是这种处理可能造成死循环,给EditValue赋值时又调用了EditValueChanged事件。
改为以下写法,注意第3个参数:
SetEditorBindingValue(txtCustomer,txtCustomer.EditValue,false);
完整的写法如下:
C# Code:
private void txtCustomer_EditValueChanged(object sender, EventArgs e)
{
//当选择客户,自动带出客户的名称、联系人、联系电话
//不给自己的EditValue赋值,参数setEditorValue=false
EditorBinding.SetEditorBindingValue(txtCustomer, txtCustomer.EditValue, false);
//根据编号编号查询客户详细资料
Customer customer = new bllCustomer().Find(txtCustomer.EditValue.ToString());
//除sender外,给其它绑定的数据源及EditValue赋值,setEditorValue=true
EditorBinding.SetEditorBindingValue(txtCustomerName, customer.CustomerName, true);
EditorBinding.SetEditorBindingValue(txtCustomerAttn, customer.Attn, true);
EditorBinding.SetEditorBindingValue(txtCustomerTel, customer.Tel, true);
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
//当选择客户,自动带出客户的名称、联系人、联系电话
//不给自己的EditValue赋值,参数setEditorValue=false
EditorBinding.SetEditorBindingValue(txtCustomer, txtCustomer.EditValue, false);
//根据编号编号查询客户详细资料
Customer customer = new bllCustomer().Find(txtCustomer.EditValue.ToString());
//除sender外,给其它绑定的数据源及EditValue赋值,setEditorValue=true
EditorBinding.SetEditorBindingValue(txtCustomerName, customer.CustomerName, true);
EditorBinding.SetEditorBindingValue(txtCustomerAttn, customer.Attn, true);
EditorBinding.SetEditorBindingValue(txtCustomerTel, customer.Tel, true);
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
SetEditorBindingValue
C# Code:
/// <summary>
/// 给绑定数据源的组件赋值
/// </summary>
/// <param name="bindingControl"></param>
/// <param name="value"></param>
/// <param name="setEditorValue"></param>
protected void SetEditorBindingValue(BaseEdit bindingControl, object value, bool setEditorValue = true)
{
if (setEditorValue)
{
//bindingControl.Text = value;
bindingControl.EditValue = value;
}
//写入数据到数据源
if (bindingControl.DataBindings.Count > 0)
{
bindingControl.DataBindings[0].WriteValue();
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// 给绑定数据源的组件赋值
/// </summary>
/// <param name="bindingControl"></param>
/// <param name="value"></param>
/// <param name="setEditorValue"></param>
protected void SetEditorBindingValue(BaseEdit bindingControl, object value, bool setEditorValue = true)
{
if (setEditorValue)
{
//bindingControl.Text = value;
bindingControl.EditValue = value;
}
//写入数据到数据源
if (bindingControl.DataBindings.Count > 0)
{
bindingControl.DataBindings[0].WriteValue();
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761

扫一扫加微信:


版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网