【问题标题】:FindControl doesn't work with my ChangePassword controlFindControl 不适用于我的 ChangePassword 控件
【发布时间】:2011-03-01 11:34:37
【问题描述】:

如何访问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>

任何帮助!

【问题讨论】:

    标签: c# asp.net findcontrol


    【解决方案1】:

    看看这个:http://weblogs.asp.net/sukumarraju/archive/2011/02/13/access-controls-with-in-change-password-control.aspx

    有这个例子:

    ChangeUserPassword.ChangePasswordTemplateContainer.FindControl("CancelPushButton”);
    

    HTH。

    【讨论】:

    • ^^^ 这个。它对我有用,是更优雅的解决方案。
    【解决方案2】:

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

    【讨论】:

    • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多