source should be your validator, arguments' Value property is in the textbox's value, try the following (didn't test, so it might not work):function ClientValidate(source, arguments){
if (arguments.Value == ""){
source.errormessage = "姓名不能为空。";
document.all(source.controltovalidate).focus();
arguments.IsValid = false;
return;
}
arguments.IsValid = true;
}
if (arguments.Value == ""){
source.errormessage = "姓名不能为空。";
document.all(source.controltovalidate).focus();
arguments.IsValid = false;
return;
}
arguments.IsValid = true;
}
void Page_Load(Object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.ID="abc";
Controls.Add(tb); CustomValidator cv = new CustomValidator();
cv.ControlToValidate = tb.ID;
cv.ClientValidationFunction = "ClientValidate";
//cv.ErrorMessage = "must be even!!!";
cv.ServerValidate += new ServerValidateEventHandler(ServerValidation);
Controls.Add(cv);
}void ServerValidation (object source, ServerValidateEventArgs args)
{
try
{
args.IsValid = Convert.ToInt32(args.Value.Trim()) % 2 == 0;
}
catch
{
args.IsValid = false;
}
}
</script><script language="javascript">
function ClientValidate(source, arguments)
{
if (arguments.Value %2 != 0)
{
//source.errormessage = "must be even";
source.innerHTML = "<B>must be even</B>";
document.all(source.controltovalidate).focus();
arguments.IsValid = false;
return;
}
arguments.IsValid = true;
}
</script>2. TestVTB.aspx:
<%@ Register TagPrefix="my" TagName="VTX" src="ValidatedTextBox.ascx"%>
<form runat="server">
<my:VTX id="vtx1" runat="server" />
<asp:Button id="btn" runat="server" Text="Submit" />
</form>
<BR>
<%=DateTime.Now%>
CustomValidator必须同时写ServerValidate的方法,这样ClientValidate才会有效