FindControl不适用于我的ChangePassword控件

时间:2011-03-01 11:34:38

标签: c# asp.net findcontrol

如何访问CancelPushButton

这不起作用!它总是返回null !!

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

ASP.Net代码:

<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>

任何帮助!

2 个答案:

答案 0 :(得分:6)

检查出来:http://weblogs.asp.net/sukumarraju/archive/2011/02/13/access-controls-with-in-change-password-control.aspx

有这个例子:

ChangeUserPassword.ChangePasswordTemplateContainer.FindControl("CancelPushButton”);

HTH。

答案 1 :(得分:0)

this文章中,此方法将找到所需的控件。

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