Click to See Complete Forum and Search --> : How To Set Focus....?
austin
01-14-2005, 07:28 AM
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
austin
PeOfEo
01-15-2005, 11:08 AM
Okay, 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.
ccamarat
01-20-2005, 07:08 AM
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;
}
}
}
}
-Chris
PeOfEo
01-20-2005, 09:11 PM
I 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.
ccamarat
01-21-2005, 06:04 AM
Fair enough. The 'focus' solution operates independently of your validation approach so it really shouldn’t matter which way you go.
Regarding the Validation Control objects – one could argue that the whole OOP approach is all about cutting corners – using boxed solutions to solve common problems. Sure, you might be able to do it better, but a. do you have to? And b. do you have the time / resources to? The validation controls work well for all of my projects, and I love the ability to simply ask "if (Page.isValid) { … }" rather than duplicating all that client logic on the server. Plus the client code can be disabled if it doesn’t meet your requirements.
PeOfEo
01-21-2005, 04:10 PM
I am all for the asp.net 'event oriented' controls. I love a lot of the controls. I am all for taking short cuts and cutting corners, stripping down extra code. But I will not cut corners when it causes my site's accessibility to suffer. I do not code with asp.net like I do in classic though, I am usign the .net form elements, I am using the repeater and other data controls, I use plenty of .net classes. I just write my own javascript for things like form validation.
ccamarat
01-22-2005, 04:02 AM
From a usability standpoint I don't like the default settings (as you can see in the code above), but those can be changed. Most of my development is done for internal applications, and IE appears to be pretty forgiving towards the poorly-formed code the validation controls produce.
Certainly I would advise anyone developing for a more diverse group (i.e. the Internet) to use your approach and develop their own validation scheme.