关注TabContainer中的TextBox



我在选项卡容器中有一个自定义登录屏幕

     <asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" AutoPostBack="true" OnActiveTabChanged="TabContainer1_ActiveTabChanged" CssClass="MyTabStyle">
                <asp:TabPanel ID="uxStandardTab" runat="server" HeaderText="Login">
                    <ContentTemplate>
                        <table width="320px" border="0" style="border-width: 0px;">
                            <tr>
                                <td>
                                    <cc1:LabeledTextBox ID="Username" runat="server" Required="true" LabelText="Email/User Name" LabelWidth="120" ControlWidth="150" LabelCss="FormLabelText BoldText" ValidationGroup="Standard"></cc1:LabeledTextBox>
                                    <cc1:LabeledTextBox ID="Password" runat="server" TextMode="Password" Required="true" LabelText="Password" LabelWidth="120" ControlWidth="150" LabelCss="FormLabelText BoldText" ValidationGroup="Standard"></cc1:LabeledTextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div style="margin-top: 5px; margin-right: 20px; text-align: center;">
                                        <asp:Button ID="btnLogin" runat="server" Text="Log In" CssClass="grnbutton" OnClick="btnLogin_Click" ValidationGroup="Standard" />
                                    </div>
                                </td>
                            </tr>
                        </table>
                    </ContentTemplate>
                </asp:TabPanel>
</asp:TabContainer>

我正在尝试将焦点设置在页面加载中的"用户名"字段上。

我试过

var txtUserName = uxStandardTab.FindControl("UserName");
        if (txtUserName != null)
        {
            Page.SetFocus(txtUserName);
        }

Username.Focus();

但是,在页面加载过程中,它们都没有将光标放在"用户名文本"框中。如果Tab控件的行为不同,请提供建议。

使用Ajax时不要使用Page.SetFocus,而是使用Scriptmanager.SetFocus:

ScriptManager.GetCurrent(Page).SetFocus(txtUserName);

最新更新