自定义文本框控件,包含Name跟ID
2008-07-11 00:40
225 查看
我们项目需要开发一个特殊的文本框,当点击文本框的时候弹出一个产品的窗口,选择一个产品之后,窗口关闭,把产品的名称跟产品的标识保存在文本框中,单击保存按钮处理单击事件的后台程序可以获取到选中产品的标识。
有了这样的需求后,让我们来开始设置这个控件。
第一步:在原有的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;
}
}
[align=left]到这里,就可以实现一个文本框即可以获取产品的名称同时也可以获取产品的标识。[/align]
有了这样的需求后,让我们来开始设置这个控件。
第一步:在原有的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;
}
}
[align=left]到这里,就可以实现一个文本框即可以获取产品的名称同时也可以获取产品的标识。[/align]
相关文章推荐
- android 布局文件中控件ID、name标签属性的命名包含“@”、“.”、“+”等等符号的含义
- android 布局文件中控件ID、name标签属性的命名包含“@”、“.”、“+”等等符号的含义
- android 布局文件中控件ID、name标签属性的命名包含“@”、“.”、“+”等等符号的含义
- android 布局文件中控件ID、name标签属性的命名包含“@”、“.”、“+”等等符号的含义
- C# webBrowser控件 如何查找没有id没有name的元素
- html控件中id与name属性的区别
- DataForm中包含TabControl控件,FindNameInContent方法无法获取未选中TabItem里的控件问题
- html控件的id和name属性有什么不同
- 如何让DevExpress.TreeList单元格中的自定义控件包含标签
- 控件包含代码块(即 <% ... %>),因此无法修改控件集合(用户自定义控件中)
- HTML控件ID和NAME属性的区别,以及如何在asp.net页面的.CS文件中获得.ASPX页面中HTML控件的值
- javascript 获取所有id中包含某关键字的控件的实现代码
- [自定义服务器控件] 第一步:文本框。
- HTML控件ID和NAME属性及在CS页面获得.ASPX页面中HTML控件的值
- 如何得到 引起onclick事件的控件(这里是checkbox)的id,name等属性
- 开发自定义字段时一定要注意复合控件的ID问题
- HTML控件ID和NAME属性的区别,以及如何在asp.net页面的.CS文件中获得.ASPX页面中HTML控件的值
- 自定义验证(可以验证输入的是否为数字、浮点数、文本框中字符的长度、日历控件)
- 如何在fragment中获取自定义view的控件id
- [原]Silverlight两种自定义包含内容(Content)控件的方案