if(用户没输入)
验证控件无效
else
验证控件有效验证控件有个属性,设置成false就可以了.
验证控件无效
else
验证控件有效验证控件有个属性,设置成false就可以了.
解决方案 »
- 请问asp.net mvc模式,模型可以自己写吗?
- 求助.. ASP.NET 如何使得系统调用默认的应用软件打开链接的文件。..
- vs2008在使用System.Data.OracleClient连接数据库中文字符出现乱字符,急呀
- 为什么artcontrols.dll里面的button不能正常显示?
- javascript联动 选值问题
- 问个asp.net2.0不刷新的问题
- Ajax可否实现后台刷新?
- DataView1.RowFilter的问题没人能解决?不会吧?
- ASP.NET应该叫ADO.NET!!!!!!!!!!!
- javascript 在asp.net 做折叠导航遇到的问题,请教各位高手
- ASP.NET中的GridView中如何使用AJAX?
- 数据绑定的时候使用数据视图
{ #region 枚举数据
/// <summary>
/// 验证数据类型
/// </summary>
public enum CDataType
{
Never, //不验证
String, //字符串
Int, //整数
IntPostive, //大于0的整数
IntZeroPostive, //大于等于0的整数
Custom
}
#endregion /// Attribute DefaultProperty指定组件的默认属性,ToolboxData指定当从IDE工具中的工具箱中拖动自定义控件时为它生成的默认标记
[DefaultProperty("AllowEmpty"), ToolboxData("<{0}:CTextBox runat=server></{0}:CTextBox>")]
//类CTextBox派生自TextBox
public class CTextBox : System.Web.UI.WebControls.TextBox
{
#region 子控件
//private System.Web.UI.WebControls.TextBox txtDataInput = new TextBox();
private System.Web.UI.WebControls.RequiredFieldValidator rfvDataInput = new RequiredFieldValidator();
private System.Web.UI.WebControls.RegularExpressionValidator revDataInput = new RegularExpressionValidator();
private Panel pnlFrame = new Panel(); //承载其它控件的容器Panel控件
#endregion private string error = ""; #region 控件自定义属性 [Bindable(true)]
[Category("自定义信息区")]
[Browsable(true)]
[Description("是否允许空值")]
[DefaultValue("true")]
public bool AllowEmpty
{
get { return ViewState["AllowEmpty"] == null ? true : (bool)ViewState["AllowEmpty"]; }
set {
ViewState["AllowEmpty"] = value;
//样式设定
if (this.AllowEmpty)
{
this.BackColor = System.Drawing.Color.LightBlue;
}
else
{
this.BackColor = System.Drawing.Color.White;
}
}
}
[Bindable(true)]
[Category("自定义信息区")]
[Browsable(true)]
[Description("验证数据类型,默认为不验证")]
[DefaultValue("IntPostive")]
public CDataType ValidType
{
get { return ViewState["ValidType"] == null ? CDataType.Never : (CDataType)ViewState["ValidType"]; }
set { ViewState["ValidType"] = value; }
}
[Bindable(true)]
[Browsable(true)]
[Category("自定义信息区")]
[Description("自定义验证错误信息")]
[DefaultValue("")]
public string ValidError
{
get { return ViewState["ValidError"] == null ? "" : (string)ViewState["ValidError"]; }
set { ViewState["ValidError"] = value; }
}
[Bindable(true)]
[Browsable(true)]
[Category("自定义信息区")]
[Description("自定义用于验证的正则表达式,ValidType 为 Custom 时有效")]
[DefaultValue("")]
public string ValidExpressionCustom
{
get { return ViewState["ValidExpressionCustom"] == null ? "" : (string)ViewState["ValidExpressionCustom"]; }
set { ViewState["ValidExpressionCustom"] = value; }
}
[Bindable(true)]
[Browsable(true)]
[Category("自定义信息区")]
[Description("错误信息提示的CSS类名")]
[DefaultValue("")]
public string CssError
{
get { return ViewState["CssError"] == null ? "" : (string)ViewState["CssError"]; }
set { ViewState["CssError"] = value; }
}
/// <summary>
/// 重写ReadOnly属性,设定样式
/// </summary>
public override bool Enabled
{
set
{
base.Enabled = value;
if (base.Enabled)
{
base.BackColor = System.Drawing.Color.White;
}
else
{
base.BackColor = System.Drawing.Color.LightGray;
}
}
} #endregion #region 构造函数
public CTextBox() { }
#endregion #region EnsureChildControls
protected override void EnsureChildControls()
{
this.rfvDataInput.CssClass = this.CssError;
this.rfvDataInput.ErrorMessage = this.ValidErrorEmpty;
//this.rfvDataInput.ErrorMessage = "*输入不能为空";
this.rfvDataInput.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
this.rfvDataInput.EnableViewState = true;
this.rfvDataInput.ControlToValidate = base.ID;
this.revDataInput.CssClass = this.CssError;
this.revDataInput.ErrorMessage = "*输入格式错误";
//this.revDataInput.ErrorMessage = ValidErrorEmpty;
this.revDataInput.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
this.revDataInput.EnableViewState = true;
this.revDataInput.ControlToValidate = base.ID;
//将子控件添加到此自定义控件中
this.Controls.Add(rfvDataInput);
this.Controls.Add(revDataInput);
this.Controls.Add(pnlFrame);
}
#endregion #region GetRegex
/// <summary>
/// 根据设置的验证数据类型返回不同的正则表达式样
/// </summary>
/// <returns></returns>
private string GetValidRegex()
{
string regex = @"(\S)";
switch (this.ValidType)
{
case CDataType.Never:
break;
case CDataType.Int:
error = "*必须为整数";
regex = @"(-)?(\d+)";
break;
case CDataType.IntPostive:
error = "*必须为大于0的整数";
regex = @"([1-9]{1}\d*)";
break;
case CDataType.IntZeroPostive:
error = "*必须为不小于0的整数";
regex = @"(\d+)";
break;
case CDataType.Float:
error = "*必须为数字";
regex = @"(-)?(\d+)(((\.)(\d)+))?";
break;
case CDataType.FloatPostive:
error = "*必须为大于0的数字";
regex = @"(\d+)(((\.)(\d)+))?";
break;
case CDataType.Custom:
error = "*格式错误";
regex = this.ValidExpressionCustom;
break;
default:
break;
}
if (this.ValidError.Trim() != "")
error = this.ValidError;
return regex;
}
#endregion #region 将此控件呈现给指定的输出参数
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
output.Write(" ");
if (!this.AllowEmpty)
{
this.rfvDataInput.ID = "rfv" + base.ID;
this.rfvDataInput.ControlToValidate = base.ID;
// add zhang
this.rfvDataInput.ErrorMessage = this.ValidErrorEmpty;
//--
this.rfvDataInput.RenderControl(output);
}
if (this.ValidType != CDataType.Never && this.ValidType != CDataType.String)
{
this.revDataInput.ID = "rev" + base.ID;
this.revDataInput.ControlToValidate = base.ID;
this.revDataInput.ValidationExpression = this.GetValidRegex();
this.revDataInput.ErrorMessage = error;
this.revDataInput.RenderControl(output);
}
}
#endregion }
}