2

I cannot access any of my asp controls in my codebehind. The .aspx page is as follows

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ChangePassword.aspx.cs" Inherits="ChangePassword" ValidateRequest="false"  %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ChangePassword ID="ChangePassword1" runat="server" CssClass="MinutesList" 
    onchangedpassword="ChangePassword1_ChangedPassword">
    <ChangePasswordTemplate>
        <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
            <tr>
                <td>
                    <table cellpadding="0">
                        <tr>
                            <td align="center" colspan="2">
                                Change Your Password</td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="CurrentPasswordLabel" runat="server" 
                                    AssociatedControlID="CurrentPassword">Password:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="CurrentPassword" runat="server" TextMode="Password" Visible="false" >></asp:TextBox>
                                <asp:RequiredFieldValidator ID="CurrentPasswordRequired" runat="server" 
                                    ControlToValidate="CurrentPassword" ErrorMessage="Password is required." 
                                    ToolTip="Password is required." ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="NewPasswordLabel" runat="server" 
                                    AssociatedControlID="NewPassword">New Password:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="NewPassword" runat="server" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="NewPasswordRequired" runat="server" 
                                    ControlToValidate="NewPassword" ErrorMessage="New Password is required." 
                                    ToolTip="New Password is required." ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="ConfirmNewPasswordLabel" runat="server" 
                                    AssociatedControlID="ConfirmNewPassword">Confirm New Password:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="ConfirmNewPassword" runat="server" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="ConfirmNewPasswordRequired" runat="server" 
                                    ControlToValidate="ConfirmNewPassword" 
                                    ErrorMessage="Confirm New Password is required." 
                                    ToolTip="Confirm New Password is required." ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2">
                                <asp:CompareValidator ID="NewPasswordCompare" runat="server" 
                                    ControlToCompare="NewPassword" ControlToValidate="ConfirmNewPassword" 
                                    Display="Dynamic" 
                                    ErrorMessage="The Confirm New Password must match the New Password entry." 
                                    ValidationGroup="ChangePassword1"></asp:CompareValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2" style="color:Red;">
                                <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Button ID="ChangePasswordPushButton" runat="server" 
                                    CommandName="ChangePassword" Text="Change Password" 
                                    ValidationGroup="ChangePassword1" />
                            </td>
                            <td>
                                <asp:Button ID="CancelPushButton" runat="server" CausesValidation="False" 
                                    CommandName="Cancel" Text="Cancel" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </ChangePasswordTemplate>
    <SuccessTemplate>
        <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
            <tr>
                <td>
                    <table cellpadding="0">
                        <tr>
                            <td align="center" colspan="2">
                                Change Password Complete</td>
                        </tr>
                        <tr>
                            <td>
                                Your password has been changed!</td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Button ID="HomePageButton" runat="server" Text="Continue" CausesValidation="true" 
                                    onclick="HomePageButton_Click" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </SuccessTemplate>
</asp:ChangePassword>
</asp:Content>

The codebehind is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ChangePassword : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    //TextBox password = (TextBox)ChangePassword1.FindControl("CurrentPassword");
    //Label passwordLabel = (Label)ChangePassword1.FindControl("CurrentPasswordLabel");
    TextBox password = ChangePassword1.FindControl("CurrentPassword") as TextBox;
    Label passwordLabel = ChangePassword1.FindControl("CurrentPasswordLabel") as Label;
    //TextBox CurrentPassword = ChangePassword1.FindControl("CurrentPassword") as TextBox;
    if (Request.QueryString["p"] != null)
    {
        passwordLabel.Visible = false;
        password.TextMode = TextBoxMode.SingleLine;
        password.Visible = false;
        password.Text = Request.QueryString["p"];
    }
}
protected void ChangePassword1_ChangedPassword(object sender, EventArgs e)
{

}
protected void HomePageButton_Click(object sender, EventArgs e)
{
    //Link.ToHomePage();
    Response.Redirect("HomePage.aspx");
}

}

The problem I am having is I get a object reference not set to an instance of an object on passwordLabel and password. Does anyone know how to get these into scope so that I can edit there text and visibility properties in the codebehind?

2 Answers 2

4

Try using:

  TextBox password = ChangePassword1.Controls[0].FindControl("CurrentPassword") as TextBox;
  Label passwordLabel = ChangePassword1.Controls[0].FindControl("CurrentPasswordLabel") as Label;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Adil Mammadov, it worked perfectly. Can you explain why this worked and the other ways did not?
I am glad that it worked. I think it is due to you have to templates in ChangePassword1 : ChangePasswordTemplate and SuccessTemplate. Because your TextBox and Label are in the first template you have to write Controls[0]. If they were in SuccessTempate it would be Controls[1]. May be I am mistaken, please try getting "HomePageButton" in SuccessTemplate and write Controls[1]. If it works that means I am not mistaken :)
1

Using ChangePasswordTemplateContainer instead of the Controls[0] won't be subject to changing the order of the templates in the control. For instance... adding a SuccessTemplate before the ChangePasswordTemplate will cause the Controls[0] code to break.

ChangePassword1.ChangePasswordTemplateContainer.FindControl("CurrentPassword")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.