作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的网页上有一个登录控件和一个创建用户控件...我希望在页面加载时光标位于登录控件的用户名文本框中...我该怎么做?
<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>
<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 oneTemplate
(anonymous or loggedIn ) is applied on theControl
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/
我是一名优秀的程序员,十分优秀!