Search code examples
c#asp.netfindcontrol

FindControl doesn't work with my ChangePassword control


How can I access to the CancelPushButton ?

This doesn't work ! it returns always null !!

var cancelButton = ChangeUserPassword.FindControl("CancelPushButton");

ASP.Net code:

<ChangePasswordTemplate>
    <span class="failureNotification">
        <asp:Literal ID="FailureText" runat="server"></asp:Literal>
    </span>
    <asp:ValidationSummary ID="ChangeUserPasswordValidationSummary" runat="server" CssClass="failureNotification" 
         ValidationGroup="ChangeUserPasswordValidationGroup"/>
    <div class="accountInfo">

        <p class="submitButton">
            <asp:Button ID="CancelPushButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"/>
            <asp:Button ID="ChangePasswordPushButton" runat="server" CommandName="ChangePassword" Text="Change Password" 
                 ValidationGroup="ChangeUserPasswordValidationGroup"/>
        </p>
    </div>
</ChangePasswordTemplate>

Any help!


Solution

  • From this article, this method will find the required control.

    public static Control FindControlRecursive(Control Root, string Id)
    {
        if (Root.ID == Id)
            return Root;
    
        foreach (Control Ctl in Root.Controls)
        {
            Control FoundCtl = FindControlRecursive(Ctl, Id);
            if (FoundCtl != null)
                return FoundCtl;
        }
    
        return null;
    }