服务器在Page_Load中控制null

时间:2011-05-24 19:11:56

标签: asp.net controls pageload

我有一个页面,里面有一些服务器控件。由于某些原因,这些控件在页面的Page_Load事件处理程序中为空。

    <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检查中的第一行得到一个空引用。我不知道这是怎么可能的。

2 个答案:

答案 0 :(得分:0)

您继承的页面是否使用标记文件(例如LogOnBasePage.aspx)?

如果是这样,那么我认为你遗憾地继承了它。标记页面生成一个继承自代码隐藏页面中定义的类的类。此生成的类的一个职责是实例化标记中定义的控件。如果从LogOnBasePage继承,则新页面将没有从LogOnBasePage标记实例化控件所需的代码,因此您将获得空引用。

答案 1 :(得分: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");
}