How To Set Focus....?

windows

Guest
how to set focus to some perticular webconrol...exclusivly..
that is like when we click on button we want to set focus on a textbox.....

so let me tell u my exact problem...i created a login form
for form authentication purpose but when the credentials are wrongly entered .... i want to set the focus on password text box so that it can be entered again...

thanks in advance.

hoping for a inovative solution

austinOkay, do your form validation in javascript and do not use the asp.net controls. This will allow you to do it, because the only way to play with the focus is js. But if you do this you will need to redo the validation server side with asp.net for those who do not support js (all this means is run a few if statements). If you just want to do it server side and only server side you can write the text box back out with a different border or background color or something too.If I'm reading this question correctly I disagree. As I read it, you're making a trip to the server to verify the user's credentials, and then bring him back if he goofed. On the post-back you want to set the focus on the Password field.
Yes, you need js to play with focus, but that doesn't mean you have to write off the .NET validation controls (they will cause you to lose focus if you choose to use them).
First, write your script to set the focus (it can be simple - document.getElementById(focusControl).focus(); )
Next, move that code to the code-behind and save it as a string. Use "RegisterStartupScript" to tell the code to run on startup. You can manipulate the focusControl variable programatically. Here's a sample implementation:

ValidationAndFocus.aspx<%@ Page language="c#" Codebehind="ValidationAndFocus.aspx.cs"
AutoEventWireup="false" Inherits="Experiments.ValidationAndFocus" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ValidationAndFocus</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<asp:Label id="lblUserName" runat="server">User Name: </asp:Label>
<asp:TextBox id="tbUserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
id="rfvUserName"
runat="server" ErrorMessage="Username cannot be blank!"
ControlToValidate="tbUserName">*</asp:RequiredFieldValidator></P>
<P>
<asp:Label id="lblPw" runat="server">Password: </asp:Label>
<asp:TextBox id="tbPW" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
id="rfvPassword"
runat="server" ErrorMessage="Password cannot be blank!"
ControlToValidate="tbPW">*</asp:RequiredFieldValidator></P>
<asp:Button id="btnLogin" runat="server" Text="Login"></asp:Button>
<asp:ValidationSummary id="rfvAll"
runat="server" ShowMessageBox="True"
ShowSummary="False"></asp:ValidationSummary>
<asp:Label id="lblStatus" runat="server"></asp:Label>
</form>
</body>
</HTML>Code-Behind:using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Experiments
{
/// <summary>
/// Summary description for ValidationAndFocus.
/// </summary>
public class ValidationAndFocus : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblUserName;
protected System.Web.UI.WebControls.TextBox tbUserName;
protected System.Web.UI.WebControls.Label lblPw;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvUserName;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvPassword;
protected System.Web.UI.WebControls.ValidationSummary rfvAll;
protected System.Web.UI.WebControls.Button btnLogin;
protected System.Web.UI.WebControls.Label lblStatus;
protected System.Web.UI.WebControls.TextBox tbPW;

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack)
{
setInitialFocus(this.tbPW.ID);
}
else
{
setInitialFocus(this.tbUserName.ID);
}
}

private void setInitialFocus(string elmId)
{
string s = @"<script>
document.getElementById(""" + elmId + @""").focus();
</script>";
this.RegisterStartupScript("InitialFocus", s);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnLogin_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
this.lblStatus.Text = "Good!<br>Password field should have focus...";
}
else
{
this.rfvAll.ShowSummary = true;
}
}
}
}
-ChrisI dislike the .net validation controls for code validity / accessibility / semantic reasons. I would rather not use something that writes out code that is incorrect (the validator controls use js without the type attribute). It is worth it to me to do it by hand instead of cutting corners.Fair enough. The 'focus' solution operates independently of your validation approach so it really shouldn鎶
 
Back
Top