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;
}

解决方案 »

  1.   

    我刚才新做一个页面 试了一下:只要是设置了 ControlToValidate="txtUserName" 根本还是不走你的客户端代码难道和我的版本有关系吗?我是2002中文版。还请 saucer(思归) 老大试一下。
      

  2.   

    实在是没有道理的啊  设置了ControlToValidate就不走客户端的验证代码?!
      

  3.   

    if the value in the textbox is empty, the validator is not triggeredI am being lazy, so instead of writing a custom control, here is a user control example:1. ValidatedTextBox.ascx:<script language="C#" runat="server">
    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%>
      

  4.   

    if you are validating something else, consider to add a RequiredFieldValidator too
      

  5.   

    多谢 好象是这样的:
    CustomValidator必须同时写ServerValidate的方法,这样ClientValidate才会有效