gpt4 book ai didi

asp.net - 默认情况下如何将焦点设置为控件?

转载 作者:行者123 更新时间:2023-12-04 06:27:45 25 4
gpt4 key购买 nike

我在我的网页上有一个登录控件和一个创建用户控件...我希望在页面加载时光标位于登录控件的用户名文本框中...我该怎么做?

<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
Bingo..!!! Youuuuu did it...<asp:LoginName ID="LoginName1" runat="server" />.



</LoggedInTemplate>
<AnonymousTemplate>

<asp:DropShadowExtender ID="DropShadowExtender1" runat="server"
TargetControlID="Panel1"
Rounded="true"
Opacity=".38">
</asp:DropShadowExtender>
<asp:Panel ID="Panel1" runat="server"
BackColor="Silver">
<asp:Login ID="Login1" runat="server"
DestinationPageUrl="~/ViewCart_aspx/ViewCart.aspx"
Height="152px"
Width="396px"
RememberMeSet="True"
OnLoggedIn="ContinueButton_Click" >
<LayoutTemplate>
<fieldset>
<table border="0"
cellpadding="1"
cellspacing="0"
style="border-collapse:collapse;">
<tr>
<td>
<table border="0" cellpadding="0" style="height:152px;width:396px;">
<tr>
<td align="center" colspan="2">
<h3>Log In</h3> </td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
</td>
<td>&nbsp;
<asp:TextBox ID="UserName" runat="server" Width="150px" TabIndex="0"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server"
ControlToValidate="UserName" ErrorMessage="User Name is required."
ToolTip="User Name is required." ValidationGroup="ctl01$Login1">*</asp:RequiredFieldValidator>
</td>
</tr>

如您所见 UserName文本框在登录控件内..所以我无法访问它的属性..我如何找到控件?

最佳答案

编辑:正如您在评论中提到的,您希望将登录按钮设置为默认按钮。为此,您需要将此按钮设置为默认按钮。

不幸的是,您没有按照我在对您的问题的评论中提出的问题正确格式化您的代码。所以我假设登录按钮位于与用户名文本框相同的名称容器中,其名称为 btnLogin您可以使用 HtmlForm.DefaultButton 将此控件设置为默认控件属性,所以:

您可以使用 Page.SetFocus 为了这。它将浏览器焦点设置为指定的控件:

Page.SetFocus(txtName);

如果您想联系您的 UserName文本框,你可以只使用:
var login1 = LoginView1.FindControl("Login1") as Login;
if (login1 != null)
{
var txtUserName = login1.FindControl("UserName");
if (txtUserName != null)
{
Page.SetFocus(txtUserName);
}

var btnLogin = login1.FindControl("btnLogin");
if (btnLogin != null)
{
Page.Form.DefaultButton = btnLogin.UniqueID;
}
}

但请注意:

For the LoginView control, when being added onto a page, at a certain time, only one Template (anonymous or loggedIn ) is applied on the Control instance, so at that time, we can only retrieve the reference of those controls in the active template( can't access those in the non-active template).

关于asp.net - 默认情况下如何将焦点设置为控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5832169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com