我有2个文本框,一个用于登录另一个用于密码 我也有1个自定义服务器端验证器,如果密码输入错误,则验证器被触发 我想要的是将注意力集中在这个错误信息上。
我也尝试了focus()
和setfocusonerror
属性。但它不起作用。
这是我的代码:
<asp:CustomValidator ID="cvLogin"
runat="server"
Display="Dynamic"
CssClass="login-req"
OnServerValidate="cvLogin_ServerValidate"
ValidationGroup="tovalidate"
SetFocusOnError="True">
</asp:CustomValidator>
protected void cvLogin_ServerValidate(object source,
ServerValidateEventArgs args)
{
if (currentUser == null)
{
args.IsValid = false;
MessageBox1.Focus();
MessageBox1.SetMessage(Resources.RSResource.Login_InvalidLogin,
Constants.MessageType.Error);
}
答案 0 :(得分:2)
尝试Page
的{{3}}方法:
if (currentUser == null)
{
args.IsValid = false;
Page.SetFocus(MessageBox1);
...
}
我不确定。但您可能需要将控件置于Panel
。
更新2:
我检查了SetFocus
方法。它适用于TextBoxes
,但不适用于Labels
或Validators
。因此,如果您想要关注消息,最好的选择是使用javascript:
document.getElementById('MessageBox1').scrollIntoView(true);
我还测试了这个服务器端代码。它工作正常:
Page.ClientScript.RegisterStartupScript(this.GetType(), "FocusScript", "document.getElementById('MessageBox1').scrollIntoView(true);", true);