📄 lengthvalidator.cs
字号:
namespace ASPNET.StarterKit.Communities {
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
//*********************************************************************
//
// LengthValidator Class
//
// Derived class from custom validator that checks length of input.
//
//*********************************************************************
public class LengthValidator : CustomValidator {
//*********************************************************************
//
// MaxLength Property
//
// The Maximum Length that a user is allowed to enter.
//
//*********************************************************************
public int MaxLength {
get {
if (ViewState["MaxLength"] == null)
return 0;
else
return (int)ViewState["MaxLength"];
}
set { ViewState["MaxLength"] = value; }
}
//*********************************************************************
//
// OnPreRender Property
//
// This is where we add the client-side script.
//
//*********************************************************************
override protected void OnPreRender(EventArgs e) {
base.OnPreRender(e);
if (RenderUplevel) {
if (!Page.IsStartupScriptRegistered("validateLength")) {
string validateLength = "<script language=\"JScript\">"
+ "function validateLength(source, arguments) { "
+ "if (arguments.Value.length > " + MaxLength + ") "
+ " arguments.IsValid = false; "
+ "else "
+ " arguments.IsValid = true; } "
+ "</script>";
Page.RegisterStartupScript("validateLength", validateLength);
}
}
}
//*********************************************************************
//
// OnServerValidate Method
//
// Server-side function for checking length.
//
//*********************************************************************
override protected bool OnServerValidate(string value) {
if (value.Length > MaxLength)
return false;
else
return true;
}
//*********************************************************************
//
// LengthValidator Constructor
//
// Assign the client validation function.
//
//*********************************************************************
public LengthValidator():base() {
ClientValidationFunction = "validateLength";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -