使用FindControl

时间:2011-10-15 14:00:15

标签: asp.net vb.net

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" CreateUserButtonText="Register">
    <WizardSteps>
        <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
            <ContentTemplate>
                <table class="style1">
                    <!-- snip -->
                    <td>
                        <asp:TextBox ID="UserName" runat="server" /><br />

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
                            ControlToValidate="UserName" ErrorMessage="Username is required." 
                            Font-Size="Small" Display="Dynamic" ToolTip="Username is required.">
                            Username is required.
                        </asp:RequiredFieldValidator>
                    </td>
                </tr>
                <!-- snip -->
            </table>
            <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
        </ContentTemplate>
        </asp:CreateUserWizardStep>
        <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
        </asp:CompleteWizardStep>
    </WizardSteps>
</asp:CreateUserWizard>

如何使用FindControl查找UserName TextBox

我试过了:

  • Dim createuserwizard = CType(sender, CreateUserWizard)
  • Dim userNameTextBox As TextBox = CType(createuserwizard.CreateUserStep.ContentTemplateContainer.FindControl("UserName"), TextBox)
  • Dim userNameTextBox As TextBox = CType(CreateUserWizardStep1.FindControl("UserName"), TextBox)

但这些都不适合我。

1 个答案:

答案 0 :(得分:1)

UserWizardStep的子控件是一个CreateUserStepContainer控件,因此您必须从那里使用FindControl。这将有效...

Dim userNameTextBox as TextBox = CType(CreateUserWizardStep1.Controls(0).FindControl("UserName"), TextBox)