2011-05-24 62 views
0

我有一個頁面,其中包含一些服務器控件。由於某些原因,這些控件在頁面的Page_Load事件處理函數中爲null。Page_Load中的服務器控件null

<asp:Content ID="mainContent" runat="server" ContentPlaceHolderID="mainPlaceholder"> 
    <asp:Label ID="userIdLockedLabel" runat="server" EnableViewState="False" ForeColor="Red" 
     Visible="False"> 
    </asp:Label> 
<asp:Panel ID="standardLoginPanel" runat="server"> 
    <asp:Login ID="loginControl" runat="server" 
     DisplayRememberMe="false" 
     PasswordRecoveryUrl="?action=lostpass" 
     TextBoxStyle-Font-Names="verdana" 
     TextBoxStyle-Font-Size="Small" 
     DestinationPageUrl="Home.aspx" 
     OnLoggingIn="OnLoggingIn" 
     OnLoginError="OnError" 
     TitleText="" > 
     <TextBoxStyle Font-Names="verdana" Font-Size="Small"></TextBoxStyle> 
     <LoginButtonStyle CssClass="btn" /> 
    </asp:Login> 
</asp:Panel> 
<asp:Panel ID="canadaLoginPanel" runat="server" Visible="false"> 
    <asp:Label ID="userFailureLabel" runat="server"></asp:Label> 
    <table> 
     <tr> 
      <td><%= Utility.RetrieveResource("CompanyNumberLabel") %></td> 
      <td><asp:TextBox ID="companyNumberTextbox" runat="server"></asp:TextBox></td> 
     </tr> 
     <tr> 
      <td><%= Utility.RetrieveResource("UserIdLabel")%></td> 
      <td><asp:TextBox ID="userIdTextbox" runat="server"></asp:TextBox></td> 
     </tr> 
     <tr> 
      <td><%= Utility.RetrieveResource("PasswordLabel")%></td> 
      <td><asp:TextBox ID="userPasswordTextbox" runat="server" TextMode="Password"></asp:TextBox></td> 
     </tr> 
     <tr> 
      <td colspan="2"> 
       <asp:CheckBox ID="rememberMeCheckBox" runat="server" /> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="2" style="text-align:center"><asp:Button ID="userSubmit" runat="server" CssClass="btn" /></td> 
     </tr> 
     <tr> 
      <td colspan="2" style="text-align:center"><a href="ForgotPassword.aspx"><%= Utility.RetrieveResource("ForgotPasswordLinkText") %></a></td> 
     </tr> 
    </table> 
</asp:Panel> 
    <br /> 
    <div style="font-family: Verdana; font-size: small"> 
     <span class="bold"> 
      <%= Utility.RetrieveResource("TroubleLogginIn") %> 
     </span><br /> 
     <%= Utility.RetrieveResource("ForgotPasswordText") %> 
    </div> 

頁面,login.aspx的從LogOnBasePage繼承,而這正是在Page_Load代碼。在那裏,我使用下面的代碼:

   if (this.Company.SiteType == Ceridian.KB.Entities.SiteType.CAN) 
      { 
       this.FindControlRecursive("standardLoginPanel").Visible = false; 
       this.FindControlRecursive("canadaLoginPanel").Visible = true; 
       ((Button)this.FindControlRecursive("userSubmit")).Text = Utility.RetrieveResource("LoginButtonText"); 
       this.Login = null; 
       CheckBox rememberMe = (CheckBox)this.FindControlRecursive("rememberMeCheckBox"); 
       rememberMe.Text = Utility.RetrieveResource("RememberMeText"); 
      } 

這裏是FindControlRecursive方法的內容。

 public static Control FindControlRecursive(this Control control, string controlId) 
    { 
     if (string.Compare(control.ID, controlId, StringComparison.OrdinalIgnoreCase) == 0) 
     { 
      // We found the control! 
      return control; 
     } 
     else 
     { 
      // Recurse through ctrl's Controls collections 
      foreach (Control child in control.Controls) 
      { 
       Control lookFor = FindControlRecursive(child, controlId); 

       if (lookFor != null) 
       { 
        // We found the control 
        return lookFor; 
       } 
      } 

      // If we reach here, control was not found 
      return null; 
     } 
    } 

我總是在if檢查中的第一行得到空引用。我不明白這是可能的。

回答

0

您正在繼承的頁面是否使用標記文件(例如LogOnBasePage.aspx)?

如果是這樣,那麼我認爲你是從運行中失去的運氣。標記頁面會生成一個繼承自頁面代碼中定義的類的類。這個生成的類的職責之一是實例化在標記中定義的控件。如果從LogOnBasePage繼承,則新頁面將不具有從LogOnBasePage標記實例化控件所需的代碼,因此您將獲得空引用。

+0

它沒有。 LogOnBasePage是在AppCode文件夾中定義的類。它從另一個類BasePage繼承,它最終從Page繼承。 – Nick 2011-05-25 19:08:14

0

可能是因爲您需要在頁面生命週期中稍後等待。嘗試把它放在這樣的page_prerendercomplete事件中

Page_PreRenderComplete(Object sender, EventArgs e) 
{ 
if (this.Company.SiteType == Ceridian.KB.Entities.SiteType.CAN) 
      { 
       this.FindControlRecursive("standardLoginPanel").Visible = false; 
       this.FindControlRecursive("canadaLoginPanel").Visible = true; 
       ((Button)this.FindControlRecursive("userSubmit")).Text = Utility.RetrieveResource("LoginButtonText"); 
       this.Login = null; 
       CheckBox rememberMe = (CheckBox)this.FindControlRecursive("rememberMeCheckBox"); 
       rememberMe.Text = Utility.RetrieveResource("RememberMeText"); 
}