创建具有自验证功能的textbox控件>>>>类中添加客户端脚本,以使验证行为可以不只在服务器端触发,也可以在客户端触发

MasterLRC 2003-11-18 08:25:52
已成功的代码如下:只有服务器端验证(只能输入一定范围的数字)。
using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Drawing;

namespace Wealth_ControlLibrary
{
/// <summary>
/// VTextBox 的摘要说明。
/// </summary>
public class VTextBox : System.Web.UI.WebControls.TextBox, IValidator
{
private bool _valid = true;
private string _errorMessage = "";
private string _friendlyName = "";
private bool _blankAllowed = true;
private int _minValue = Int32.MinValue;
private int _maxValue = Int32.MaxValue;
private int _value;

public string FriendlyName
{
get{return _friendlyName;}
set{_friendlyName = value;}
}
public bool AllowBlank
{
get{return _blankAllowed;}
set{_blankAllowed = value;}
}
public int MinValue
{
get{return _minValue;}
set
{
_minValue = value;
if(_minValue > _maxValue)
{
int swap = _minValue;
_minValue = _maxValue;
_maxValue = swap;
}
}
}
public int MaxValue
{
get { return _maxValue; }
set
{
_maxValue = value;

if (_minValue > _maxValue)
{
int swap = _minValue;
_minValue = _maxValue;
_maxValue = swap;
}
}
}
public VTextBox()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
Page.Validators.Add(this);
}
protected override void OnUnload(EventArgs e)
{
if(Page != null)
{
Page.Validators.Remove(this);
}
base.OnUnload (e);
}

#region IValidator 成员

public void Validate()
{
// TODO: 添加 VTextBox.Validate 实现
this.IsValid = true;

bool isBlank = (this.Text.Trim() == "");

if (isBlank)
{
if (!AllowBlank)
{
this.ErrorMessage = String.Format("'{0}' " +
"cannot be blank.", this.FriendlyName);
this.IsValid = false;
}
}
else
{
try
{
_value = Int32.Parse(this.Text);
if (_value < this.MinValue)
{
this.ErrorMessage = String.Format("'{0}' cannot " +
"be less than {1}",
this.FriendlyName, this.MinValue);
this.IsValid = false;
}

if (_value > this.MaxValue)
{
this.ErrorMessage = String.Format("'{0}' " +
"cannot be more than {1}",
this.FriendlyName, this.MinValue);
this.IsValid = false;
}
}
catch
{
this.ErrorMessage = String.Format("'{0}' " +
"is not a valid integer.", this.FriendlyName);
this.IsValid = false;
}
}
}
public bool IsValid
{
get{return _valid;}
set
{
_valid = value;
if(!_valid)
{
this.BackColor = Color.LightCoral;
}
else
{
this.BackColor = Color.White;
}
}

}

public string ErrorMessage
{
get{return _errorMessage;}
set{_errorMessage = value;}

}
public int Value
{
get { return _value; }
set
{
_value = value;
this.Text = _value.ToString();
}
}

#endregion
}
}
...全文
27 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
MasterLRC 2003-11-19
  • 打赏
  • 举报
回复
又有一扇门打开了一道缝,一束七彩的光出现在我面前。
我以前大开过这个窗口许多次,竟从来没有发现过。
能不能给个例子,谢谢!
MasterLRC 2003-11-18
  • 打赏
  • 举报
回复
想为自己量身定做一个,:)
jpyc 2003-11-18
  • 打赏
  • 举报
回复
不是有验证控件吗?
saucer 2003-11-18
  • 打赏
  • 举报
回复
apparently, you need to output javascript , see

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconbasevalidatorsample.asp

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧