首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

CustomValidator有关问题,请大家指点指点~

2012-05-20 
CustomValidator问题,请大家指点指点~~~碰到一个奇怪的问题,下面是我模拟这个问题的一个简单例子,请大家指

CustomValidator问题,请大家指点指点~~~
碰到一个奇怪的问题,下面是我模拟这个问题的一个简单例子,请大家指点

  有三个panel。panel1,panel2,panel3。页面加载隐藏panel2和panel3.点击panel1中的下一步,到达panel2。其中panel2中有一个CustomValidator验证,验证textbox中的值是否为aa。如果不是则提示“用户不存在”然后点击下一步进入panel3.
   
  问题: 验证控件不起作用,CustomValidator1_ServerValidate不执行。不知道为什么。如果只是在一个页面上加一个textbox。一个按钮,一个验证控件。它是起作用的。请大家帮忙~~谢谢。
html代码如下:

C# code
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>无标题页</title>    <style type="text/css">        .style1        {            width: 100%;        }        .style2        {            width: 630px;            text-align: right;        }    </style></head><body>    <form id="form1" runat="server">    <div>            <asp:Panel ID="Panel1" runat="server">                <table class="style1">            <tr>                <td>                    1</td>            </tr>            <tr>                <td style="text-align: center">                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"                         style="text-align: left" Text="Button" />                </td>            </tr>        </table>        </asp:Panel>                    <asp:Panel ID="Panel2" runat="server">                <table class="style1">                    <tr>                        <td class="style2">                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>                        </td>                        <td>                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"                                 ControlToValidate="TextBox1" ErrorMessage="不能为空">*</asp:RequiredFieldValidator>                            <asp:CustomValidator ID="CustomValidator1" runat="server"                                 ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="用户已存在"                                 onservervalidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>                        </td>                    </tr>                    <tr>                        <td class="style2">                            &nbsp;</td>                        <td>                            &nbsp;</td>                    </tr>                    <tr>                        <td class="style2">                            &nbsp;</td>                        <td>                            &nbsp;</td>                    </tr>                    <tr>                        <td colspan="2" style="text-align: center">                            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />                        </td>                    </tr>                </table>                    </asp:Panel>                                <asp:Panel ID="Panel3" runat="server">                        <table class="style1">            <tr>                <td colspan="2">                    3</td>            </tr>            <tr>                <td>                    &nbsp;</td>                <td>                    &nbsp;</td>            </tr>        </table>        </asp:Panel>    </div>    </form></body></html> 


C# code
    protected void Page_Load(object sender, EventArgs e)    {        Panel1.Visible = true;        Panel2.Visible = false;        Panel3.Visible = false;    }    protected void Button1_Click(object sender, EventArgs e)    {        Panel1.Visible = false;        Panel2.Visible = true;        Panel3.Visible = false;    }    protected void Button2_Click(object sender, EventArgs e)    {        if (this.IsValid)        {            Panel1.Visible = false;            Panel2.Visible = false;            Panel3.Visible = true;        }    }    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)    {        if (TextBox1.Text == "aa")        {            args.IsValid = false;        }        else        {            args.IsValid = true;        }    }


[解决办法]
C# code
  <script language="javascript" type="text/javascript">          function validateStr(oSrc, args) {              strT = document.getElementById("txtName");              strText = "aa,AA";              var strs = new Array();              strs = strText.split(",");              for (i = 0; i < strs.length; i++) {                 args.IsValid = (strs[i] != strT.value);                  if ((strs[i] != strT.value) == false) {                      break;                  }              }          }            </script>  <asp:TextBox ID="txtName" runat="server" Width="262px"></asp:TextBox>      <asp:CustomValidator ID="cvtxtName" runat="server"         ErrorMessage=" aa存在." ValidationGroup="btn"         ControlToValidate="txtName" ClientValidationFunction="validateStr"></asp:CustomValidator><asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" ValidationGroup="btn" />后台  protected void Button1_Click(object sender, EventArgs e)    {        if (IsValid)        {            //验证通过,显示Panel        }    } 

热点排行