How to set focus on a page ..

2 views
Skip to first unread message

Swaroop Joshi

unread,
Sep 29, 2005, 5:30:12 AM9/29/05
to Techdot...@googlegroups.com
Hi All,
 
How to set the focus in ASP.net for like say a textbox,Dropdownlist etc when we do validation if there is any error for incorrect input.?
How can we do this?
Can we do this both at Client and Server side?
 
Peace and Harmony...
Swaroop Jayant Joshi

"Success is simple. Do what's right, the right way, at the right time."

Thread

unread,
Sep 29, 2005, 5:35:22 AM9/29/05
to .NetIndia
Hi Swaroop,

We can do it in client side .Can do this in server side if you are
using VS 2005.
Lets check other;s opinion

Regards
Thread

Selvakumar

unread,
Sep 29, 2005, 6:16:43 AM9/29/05
to Techdot...@googlegroups.com
Hiii,
Put a ASP.Net Label control and set the font color to the label
as the background color of the form.Now from CODE set the value of the
label to the name of the control to which Focus has to be made!!!.

Add the following script to the end of the BODY tag of the web page..
<script>
//set the focus to the control
if (window.document.all("LabelName").value != "")
{
window.document.all(window.document.all("LabelName").value).focus();
window.document.all("LabelName").value = "";
}
</script>

Try it let me know if u have any doubts..This is what i've done for
focus problems...



Regards,
Selva..

Anup Shinde

unread,
Sep 29, 2005, 7:44:43 AM9/29/05
to BDOTNET, Techdot...@googlegroups.com, dotnetuse...@groups.msn.com, -netind...@groups.msn.com
Hi,

Use this method to set focus to your web-controls. Hopefully, it
should work fine in all cases


-----------

Private Function SetFocus(ByRef ctrl As
System.Web.UI.WebControls.WebControl)
Dim strScript As String

If (ctrl.Enabled And ctrl.Visible) Then
strScript = " <script language=javascript>
document.forms[0]." + TextBox2.ClientID + ".focus(); </script>"
Page.RegisterStartupScript(ctrl.ID + "_focus", strScript)
End If
End Function

-----------


Call it like: SetFocus(TextBox1)

Regards,
Anup Shinde

Shivprasad Koirala

unread,
Oct 2, 2005, 11:40:22 PM10/2/05
to .NetIndia
Whats the problems with registerstarupscript and setfocus
-------
Regards ,
C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/
My Interview Blog
http://spaces.msn.com/members/dotnetinterviews/

Reply all
Reply to author
Forward
0 new messages