日期:2014-05-19  浏览次数:20411 次

用户自定义验证控件 用autopostback无效!
aspx.cs代码
                protected   void   CheckUserName_ServerValidate(object   source,   ServerValidateEventArgs   args)
                {
                        string   UserN   =   args.Value;
                        SqlConnection   con   =   FoCode.CreateConn.CreateConnection();
                        con.Open();
                        SqlCommand   cmd   =   new   SqlCommand( "select   Count(*)   from   Fo_User   where   UserName= ' "   +   UserN   +   " ' ",   con);
                        int   countN   =   Convert.ToInt32(cmd.ExecuteScalar());
                        con.Close();
                        if   (countN   >   0)
                        {
                                args.IsValid   =   false;
                        }
                        else
                        {
                                args.IsValid   =   true;
                        }
                }
aspx代码
<asp:TextBox   ID= "UserName "   runat= "server "   AutoPostBack= "True "> </asp:TextBox> </td>
                                <td   class= "RegisterC ">
                                <asp:CustomValidator   ID= "CheckUserName "   runat= "server "   ErrorMessage= "这个用户名已存在! "   ControlToValidate= "UserName "   Display= "Dynamic "   OnServerValidate= "CheckUserName_ServerValidate "   ValidateEmptyText= "True "   OnLoad= "Page_Load "> </asp:CustomValidator> </td>

为什么用autopostback自动提交的对于自定义用户控件没反应,就和没验证一样!
怎么解决?

------解决方案--------------------
up
------解决方案--------------------
你加一个Server端的Button就OK! 然后去掉AutoPostBack= "True "