求一个自定义控件 最进做验证textbox的时候老是要求验证,单个拖控件工作量太大了谁能帮我提供1个比较简单的textbox控件,只要实现对该textbox验证不能为空或着只能输入数字 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 自己把TextBox和RequireFieldValidor封装成一个控件就行了 比较简单啊.俺是先建立一个类关系图,然后生成框架,再修改,这样可以利用vs的自动代码生成功能.using System;using System.Collections.Generic;using System.Text;using System.ComponentModel;namespace SutsComponent{ public class SutsNumberBox : System.Windows.Forms.TextBox { public SutsNumberBox() : base() { InitializeComponent(); } private void InitializeComponent() { this.SuspendLayout(); // // SutsNumberBox // this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.SutsNumberBox_KeyPress); this.TextChanged += new System.EventHandler(this.SutsNumberBox_TextChanged); this.ResumeLayout(false); } private void SutsNumberBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == '.' && Text.IndexOf('.') < 0) || e.KeyChar == '\b' || e.KeyChar == '\r' || e.KeyChar == '\t') { e.Handled = false; } else e.Handled = true; } private void SutsNumberBox_TextChanged(object sender, EventArgs e) { if (Text == "") { Text = "0"; } } public double GetValue() { try { return Convert.ToDouble(Text.Trim()); } catch { return 0; } } [Description("得到或设置文本框中的数值!"), Category("表达式")] public double DoubleValue { set { Text = value.ToString(); } get { return GetValue(); } } }} 用用户控件来做,把TEXTBOX结合JS或RequireFieldValidor来做或自己写自定义控件.类似4楼的. 复合控件,可以参考这个方法,讲得还算详细http://hi.baidu.com/5imilan/blog/item/7a2de6dd342a28305982dde6.html记得还有一篇讲得更好的,现在找不到了 可以参考discuz!NT的源码里面就有这个集成验证功能的TextBox控件 public class CustomerControl:Control { /// <summary> /// 要应用控件的状态,必须重写三个方法 方法1 OnInit() /// 在页面注册 将控件注册为持久化地控件 /// </summary> /// <param name="e"></param> protected override void OnInit(EventArgs e) { Page.RegisterRequiresControlState(this); base.OnInit(e); } protected override object SaveControlState() { return this.Text; } protected override void LoadControlState(object savedState) { this.Text = savedState.ToString(); } public virtual string Text { get { if (ViewState["Text"] != null) { return ViewState["Text"].ToString(); } return null; } set { if ((value != "") && (是否数字)) { ViewState["Text"] = value; } else { ViewState["Text"] = ""; } } } protected override void Render(HtmlTextWriter writer) { writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text); writer.AddAttribute(HtmlTextWriterAttribute.Width, "150"); writer.AddAttribute(HtmlTextWriterAttribute.Id, "text1"); writer.AddAttribute(HtmlTextWriterAttribute.Type, "text"); writer.RenderBeginTag (HtmlTextWriterTag.Input); writer.RenderEndTag (); writer.RenderEndTag (); } } 问一个NET的问题,导出EXECL表格,运环境在NET4.0,本地测试没有问题,上传服务器就有问题,提示这个错误 紧急求助:如何动态设定Button的Click事件处理方法? 大家用过。net的代码自动生成工具软件吗? 文件上传覆盖 100分求高手指点,参与讨论:关于会员系统的安全性 一个初学者的问题 jmail发邮件错误(100) 如何据用户不同显示不同内容 急!!急!!!急!!!!!啊!!! Win2000 server 下如何配置ASP.net? 母版页里能不能设置主题 Gridview中的DropDownList的数据绑定的效率问题
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;namespace SutsComponent
{
public class SutsNumberBox : System.Windows.Forms.TextBox
{
public SutsNumberBox()
: base()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// SutsNumberBox
//
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.SutsNumberBox_KeyPress);
this.TextChanged += new System.EventHandler(this.SutsNumberBox_TextChanged);
this.ResumeLayout(false); } private void SutsNumberBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') ||
(e.KeyChar == '.' && Text.IndexOf('.') < 0) ||
e.KeyChar == '\b' || e.KeyChar == '\r' || e.KeyChar == '\t')
{
e.Handled = false;
}
else
e.Handled = true;
} private void SutsNumberBox_TextChanged(object sender, EventArgs e)
{
if (Text == "")
{
Text = "0";
}
} public double GetValue()
{
try
{
return Convert.ToDouble(Text.Trim());
}
catch
{
return 0;
}
} [Description("得到或设置文本框中的数值!"), Category("表达式")]
public double DoubleValue
{
set
{
Text = value.ToString();
}
get
{
return GetValue();
}
}
}
}
或自己写自定义控件.类似4楼的.
http://hi.baidu.com/5imilan/blog/item/7a2de6dd342a28305982dde6.html
记得还有一篇讲得更好的,现在找不到了
里面就有这个集成验证功能的TextBox控件
public class CustomerControl:Control
{
/// <summary>
/// 要应用控件的状态,必须重写三个方法 方法1 OnInit()
/// 在页面注册 将控件注册为持久化地控件
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e)
{
Page.RegisterRequiresControlState(this);
base.OnInit(e);
}
protected override object SaveControlState()
{
return this.Text;
}
protected override void LoadControlState(object savedState)
{
this.Text = savedState.ToString();
}
public virtual string Text
{
get
{
if (ViewState["Text"] != null)
{
return ViewState["Text"].ToString();
}
return null;
}
set
{
if ((value != "") && (是否数字))
{
ViewState["Text"] = value;
}
else
{
ViewState["Text"] = "";
}
} }
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
writer.AddAttribute(HtmlTextWriterAttribute.Width, "150");
writer.AddAttribute(HtmlTextWriterAttribute.Id, "text1");
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.RenderBeginTag (HtmlTextWriterTag.Input);
writer.RenderEndTag ();
writer.RenderEndTag ();
} }