I am new to ASP/ASP.NET so kindly be gentle. When my index.aspx page
loads, I need the focus to be on one of the textboxes called
txtUserName.
I come from a VB background, so expecting the obvious, I go to the
Page_Load event of the index.aspx page and try to write
txtUserName.SetFocus but I see there isn't a SetFocus method for the
System.Web.UI.WebControls.TextBox class.
What's the way to set the focus to a textbox on the page load in
ASP.NET?It's a client-side task. You need to call javascript focus() method on the
element you want to set focus on.
Eliyahu
"Water Cooler v2" <wtr_clr@.yahoo.com> wrote in message
news:1124274011.476683.55980@.o13g2000cwo.googlegroups.com...
> I am new to ASP/ASP.NET so kindly be gentle. When my index.aspx page
> loads, I need the focus to be on one of the textboxes called
> txtUserName.
> I come from a VB background, so expecting the obvious, I go to the
> Page_Load event of the index.aspx page and try to write
> txtUserName.SetFocus but I see there isn't a SetFocus method for the
> System.Web.UI.WebControls.TextBox class.
> What's the way to set the focus to a textbox on the page load in
> ASP.NET?
>
you can write a function something like this and use it when u need them.
protected void SetFocus(Control controlToFocus){
string formName = GetFormName(controlToFocus);
string jsString="<script language=javascript>document." + formName +
".elements['" + controlToFocus.UniqueID + "'].focus();</script>";
if(Page.IsStartupScriptRegistered("SetFocusToSearch")==false)
Page.RegisterStartupScript("SetFocusToSearch",jsString);
Hope this helps.
--
Kannan.V
Home : http://www.kannanv.com
Blog : http://kannanv.blogspot.com
Web : http://www.DotnetLounge.net
"Any one who has never made a mistake has never tried anything new" - Einste
in
"Water Cooler v2" wrote:
> I am new to ASP/ASP.NET so kindly be gentle. When my index.aspx page
> loads, I need the focus to be on one of the textboxes called
> txtUserName.
> I come from a VB background, so expecting the obvious, I go to the
> Page_Load event of the index.aspx page and try to write
> txtUserName.SetFocus but I see there isn't a SetFocus method for the
> System.Web.UI.WebControls.TextBox class.
> What's the way to set the focus to a textbox on the page load in
> ASP.NET?
>
As the last post adviced this should do the trick:-
Private Sub SetFocus(ByVal ctrl As Control)
' Define the JavaScript function for the specified control.
Dim focusScript As String = "<script language='javascript'>" & _
"document.getElementById('" + ctrl.ClientID & _
"').focus();</script>"
' Add the JavaScript code to the page.
Page.RegisterStartupScript("FocusScript", focusScript)
End Sub
Hope this helps
Patrick
*** Sent via Developersdex http://www.examnotes.net ***
0 comments:
Post a Comment