自定义文本框控件,包含Name跟ID
我们项目需要开发一个特殊的文本框,当点击文本框的时候弹出一个产品的窗口,选择一个产品之后,窗口关闭,把产品的名称跟产品的标识保存在文本框中,单击保存按钮处理单击事件的后台程序可以获取到选中产品的标识。
有了这样的需求后,让我们来开始设置这个控件。
第一步:在原有的Textbox进行扩展
public class SelectProductEditor : TextBox
{
}
第二步:就是要考虑如何保存选中产品的标识了。这里我们是动态添加一个TextBox,让它来保持选中产品的标识。
private TextBox txtProductID;
/// <summary>
有了这样的需求后,让我们来开始设置这个控件。
第一步:在原有的Textbox进行扩展
public class SelectProductEditor : TextBox
{
}
第二步:就是要考虑如何保存选中产品的标识了。这里我们是动态添加一个TextBox,让它来保持选中产品的标识。
private TextBox txtProductID;
/// <summary>
/// 动态添加一个TextBox,让它来保持选中产品的标识
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (txtProductID == null)
{
txtProductID = new TextBox();
}
txtProductID.ID = ClientID + "ID";
txtProductID.Attributes["style"] = "display:none;";
}
第三步:就是如何得到动态生成的TextBox的Text。这里我们通过重载LoadPostData,获取动态添加TextBox的回发数值。
/// <summary>
///
/// </summary>
/// <param name="postDataKey"></param>
/// <param name="postCollection"></param>
/// <returns></returns>
protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string strTemp = postCollection[this.ClientID + "ID"];
if(string.IsNullOrEmpty(strTemp) == false)
{
txtProductID.Text= strTemp;
}
return base.LoadPostData(postDataKey, postCollection);
}
第四步:公开一个属性来获取产品标识的值
/// <summary>
/// 获取产品标识的值
/// </summary>
[DefaultValue(""), Localizable(true), Bindable(true, BindingDirection.TwoWay), Category("Appearance"), Description("TextBox_ProductID"), PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty), Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))
]
public string ProductID
{
get
{
string text1 = this.txtProductID.Text;
if (text1 != null)
{
return text1;
}
return string.Empty;
}
set
{
this.txtProductID.Text = value;
}
}
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (txtProductID == null)
{
txtProductID = new TextBox();
}
txtProductID.ID = ClientID + "ID";
txtProductID.Attributes["style"] = "display:none;";
}
第三步:就是如何得到动态生成的TextBox的Text。这里我们通过重载LoadPostData,获取动态添加TextBox的回发数值。
/// <summary>
///
/// </summary>
/// <param name="postDataKey"></param>
/// <param name="postCollection"></param>
/// <returns></returns>
protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string strTemp = postCollection[this.ClientID + "ID"];
if(string.IsNullOrEmpty(strTemp) == false)
{
txtProductID.Text= strTemp;
}
return base.LoadPostData(postDataKey, postCollection);
}
第四步:公开一个属性来获取产品标识的值
/// <summary>
/// 获取产品标识的值
/// </summary>
[DefaultValue(""), Localizable(true), Bindable(true, BindingDirection.TwoWay), Category("Appearance"), Description("TextBox_ProductID"), PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty), Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))
]
public string ProductID
{
get
{
string text1 = this.txtProductID.Text;
if (text1 != null)
{
return text1;
}
return string.Empty;
}
set
{
this.txtProductID.Text = value;
}
}
到这里,就可以实现一个文本框即可以获取产品的名称同时也可以获取产品的标识。