以下是一段验证控件的代码,我的目的是想在button1的OnClientClick事件中写一些代码,来控制id为msg的DIV的class属性为.msg,各位请帮帮忙
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <style type="text/css">
    .msg{border:1px solid #FF8080;background-color:#FFC0C0;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div id="msg" runat="server">
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
            Display="Dynamic" ErrorMessage="请输入内容" ForeColor="">请输入内容</asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
            Display="Dynamic" ErrorMessage="错误的电子邮件" ForeColor="" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">错误的电子邮件</asp:RegularExpressionValidator>
    </div>
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button OnClientClick="" ID="Button1" runat="server" Text="Button" />
   </div>
   </div>
    </form>
</body>
</html>

解决方案 »

  1.   

    补充一下:我是想在页面未通过验证的情况下,(利用页面产生的javascript.)才设置id为msg的div的class属性.谢谢.各位帮忙.
      

  2.   

    用HTML的提交,然后写验证控件的PreRender事件,对不对我也不知道了,自己试下哦private void RequiredFieldValidator1_PreRender(object sender, System.EventArgs e)
    {
    if(!this.RequiredFieldValidator1.IsValid)
    {
    string script="<script>document.getElementById('msg').className='inp33'</script>";
    Page.RegisterStartupScript("a",script);
    }
    }
      

  3.   


    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>无标题页</title>
        <style type="text/css">
        .msg{border:1px solid #FF8080;background-color:#FFC0C0;}
        </style>
        <script type="text/javascript">
           function setClass()
           {
               if(RequiredFieldValidatorEvaluateIsValid(RequiredFieldValidator1)==false
                  || RegularExpressionValidatorEvaluateIsValid(RegularExpressionValidator1)==false)
                  {
                      document.getElementById("msg").className = "msg";
                      return false;
                  }
               else
               {
                   document.getElementById("msg").className = "";
                   return true;
               }
           }
        
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <div id="msg" runat="server">
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
                Display="Dynamic" ErrorMessage="请输入内容" ForeColor="">请输入内容</asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
                Display="Dynamic" ErrorMessage="错误的电子邮件" ForeColor="" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">错误的电子邮件</asp:RegularExpressionValidator>    </div>
        <div id="liuliu">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button OnClientClick="setClass()" ID="Button1" runat="server" Text="Button" />
       </div>
       </div>
        </form>
    </body>
    </html>