FindControl doesn't work with my ChangePassword control

2k Views Asked by At

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!

2

There are 2 best solutions below

1
On BEST ANSWER

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;
}
1
On

Check this out: http://weblogs.asp.net/sukumarraju/archive/2011/02/13/access-controls-with-in-change-password-control.aspx

It has this example:

ChangeUserPassword.ChangePasswordTemplateContainer.FindControl("CancelPushButton”);

HTH.