C# 无法访问codebehind中的asp.net changepassword控件

C# 无法访问codebehind中的asp.net changepassword控件,c#,asp.net,asp.net-controls,change-password,C#,Asp.net,Asp.net Controls,Change Password,我无法访问codebehind中的任何asp控件。.aspx页面如下所示 <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ChangePassword.aspx.cs" Inherits="ChangePassword" ValidateRequest="false" %> <asp:Content ID="Content1" Co

我无法访问codebehind中的任何asp控件。.aspx页面如下所示

<%@ 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>
我遇到的问题是,我得到的对象引用没有设置为passwordLabel和password上对象的实例。是否有人知道如何将这些内容纳入范围,以便我可以编辑codebehind中的文本和可见性属性?

尝试使用:

  TextBox password = ChangePassword1.Controls[0].FindControl("CurrentPassword") as TextBox;
  Label passwordLabel = ChangePassword1.Controls[0].FindControl("CurrentPasswordLabel") as Label;

使用ChangePasswordTemplateContainer而不是控件[0]不会改变控件中模板的顺序。例如。。。在ChangePasswordTemplate之前添加SuccessTemplate将导致控件[0]代码中断

ChangePassword1.ChangePasswordTemplateContainer.FindControl(“当前密码”)


谢谢@Adil Mammadov,它工作得很好。你能解释一下为什么这个方法有效而其他方法无效吗?我很高兴它有效。我认为这是因为您必须在ChangePassword1中使用模板:ChangePasswordTemplate和SuccessTemplate。由于文本框和标签位于第一个模板中,因此必须编写控件[0]。如果他们成功的话,那就是对照组[1]。可能是我弄错了,请尝试在SuccessTemplate中获取“HomePageButton”并写入控件[1]。如果有效,这意味着我没有弄错:)
  TextBox password = ChangePassword1.Controls[0].FindControl("CurrentPassword") as TextBox;
  Label passwordLabel = ChangePassword1.Controls[0].FindControl("CurrentPasswordLabel") as Label;