当前位置: 代码迷 >> ASP.NET >> 动态加载用户控件 其中的验证控件没有工作解决办法
  详细解决方案

动态加载用户控件 其中的验证控件没有工作解决办法

热度:565   发布时间:2013-02-25 00:00:00.0
动态加载用户控件 其中的验证控件没有工作
User.Master.us:
//用户登录前后显示不同的控件
protected void Page_Load(object sender, EventArgs e)
  {
  if (Request.Cookies["UserID"] == null)
  {
  //用户未登录
  Control userControl = new Control();
  userControl = LoadControl("~/UserControls/UserLogin.ascx");
  UserPanel.Controls.Add(userControl);
  }
  else
  {
  Control userControl = new Control();
  userControl = LoadControl("~/UserControls/UserInfo.ascx");
  UserPanel.Controls.Add(userControl);
  }
  }

UserLogin.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserLogin.ascx.cs" Inherits="UserControls_UserLogin" %>
<table border="0" cellpadding="1" cellspacing="0" style="border-collapse: collapse">
  <tr>
  <td>
  <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>  
  <asp:TextBox ID="UserName" MaxLength="20" runat="server" ValidationGroup="LoginGroup"></asp:TextBox>
  </td>  
   
  <td>
  <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
  <asp:TextBox ID="Password" runat="server" MaxLength="20" TextMode="Password" ValidationGroup="LoginGroup"></asp:TextBox>
  </td>
   
  <td>
  <asp:Label ID="VerifyCodeLabel" runat="server" AssociatedControlID="VerifyCode">VerifyCode:</asp:Label>
  </td>
  <td style="width: 414px">
  <asp:TextBox ID="VerifyCode" runat="server" MaxLength="4" ToolTip="input the number in the picture right please." ValidationGroup="LoginGroup"></asp:TextBox>
  <asp:RequiredFieldValidator ID="VerifycodeRequired" runat="server" SetFocusOnError="True" ControlToValidate="VerifyCode"
  Display="Dynamic" ValidationGroup="LoginGroup">Please input the number in the picture right.</asp:RequiredFieldValidator>  
  <img id="imageCode" src="VerifyCode.aspx?flag=2" alt="please reflash the page." />
  </td>
   
  <td align="center">
  <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Sign in" OnClick="LoginButton_Click"/>
  </td>  
  </tr>
  
  <tr>
  <td colspan="4" align="center">
  <a href="Register.aspx">Sign up for free!</a>|<a href="ResetPwd.aspx">Forgotten my password?</a>
  <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" Display="Dynamic" ErrorMessage="Username requires" ValidationGroup="LoginGroup"></asp:RequiredFieldValidator>
  <asp:RegularExpressionValidator ID="REV_UserName" runat="server" ErrorMessage="UserName format illegal." 
  ControlToValidate="UserName" Display="Dynamic" ValidationExpression="^[0-9a-zA-Z]{6,20}$" SetFocusOnError="True" ValidationGroup="LoginGroup"></asp:RegularExpressionValidator>
  相关解决方案