Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Problem with Focusing the Text box on Page Load

  Asked By: Tye    Date: Jul 01    Category: Asp.net    Views: 2176
  

I read the article from

aspnet.4guysfromrolla.com/.../090902-1.2.aspx.I
made a small test
To focus on the First text Boox of the 3 called the
Key on Bodyonload of th ePage.But it failed to work
I think I am going wrong somewhere. can you
help
me.

The Test.aspx


<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="Focus4guys.aspx.vb"
Inherits="WebApplication1.Focus4guys"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">
<HTML>
<HEAD>
<title>Focus4guys</title>
<meta name="GENERATOR"
content="Microsoft Visual
Studio.NET 7.0">
<meta name="CODE_LANGUAGE"
content="Visual Basic
7.0">
<meta name="vs_defaultClientScript"
content="JavaScript">
<meta name="vs_targetSchema"

content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout"
onload=focus()>
<form id="Form1" method="post"
runat="server">
<input type="text" name="txtr">
<asp:TextBox id="TextBox1"
style="Z-INDEX: 101;
LEFT: 42px; POSITION: absolute; TOP: 8px"
runat="server"></asp:TextBox>
<asp:TextBox id="TextBox3"
style="Z-INDEX: 103;
LEFT: 44px; POSITION: absolute; TOP: 95px"
runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2"
style="Z-INDEX: 102;
LEFT: 44px; POSITION: absolute; TOP: 51px"
runat="server"></asp:TextBox>
</form>
</body>
</HTML>


The Test.aspx.vb

Public Class Focus4guys
Inherits System.Web.UI.Page
Protected WithEvents TextBox1 As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBox2 As
System.Web.UI.WebControls.TextBox
Protected WithEvents TextBox3 As
System.Web.UI.WebControls.TextBox

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()>
Private
Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.Init
'CODEGEN: This method call is required by
the
Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
RegisterStartupScript("focus", "<script
language=""JavaScript"">" & vbCrLf & _
vbTab & "Form1.TextBox1.focus();" & _
vbCrLf & vbTab & "Form1.TextBox1.select();"
&
_
vbCrLf & "<" & "/script>")

End Sub

End Class

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Amelia Schmidt     Answered On: Jul 01

The code  is ok, I would only think that there is a
javascript error
before the focus  scripts executes. And that error
could come up because
you set onload="focus()" in the body tag.

So, that's not necesary if you're using
RegisterStartupScript. just
remove the onload event handler.

A way of debug this is checkin the View Source in
the browser. You can
see if the js was generated  or not in appropiate
format.

 
Answer #2    Answered By: Kristen Chavez     Answered On: Jul 01

I have a quick question in regards to the
RegisterStartupScript and
RegisterScriptBlock methods. I understand the
RegisterStartupScript and
that works great. But what would you say is the
difference between
using RegisterScriptBlock or just using a Literal
and passing a string
of JavaScript? Ultimately the result should be same
to the browser, but
does anybody see any difference or problems between
the two?

 
Answer #3    Answered By: Jennie Harris     Answered On: Jul 01

If using RegisterClientScriptBlock than you can query if you allready register your client-side script.
Like in this example from msdn:


// Form the script that is to be registered at client side.
String scriptString = "<script language=JavaScript> function DoClick() {";
scriptString += "myForm.show.value='Welcome to Microsoft .NET'}<";
scriptString += "/";
scriptString += "script>";

if(!this.IsClientScriptBlockRegistered("clientScript"))
this.RegisterClientScriptBlock("clientScript", scriptString);

IMHO It's easyer and safer to control you code  flow like this than just fill the Literal control.

 
Answer #4    Answered By: Melissa King     Answered On: Jul 01

The difference from where the generated  script is placed in the from

RegisterClientScriptBlock places the code  just after the opening tag of
the page's object's <form runat=server>element.

RegisterStartupScript emits the script just before the closing tag of
the page  object's </form>

Startup scripts are intended to load  those client scripts where you want
immediate script action when the form  has completed loading in the
browser. If these scripts were placed in the RegisterClientScriptBlock,
the page elements the client script may want to interact would not be
present on the page. Most times, I opt for the Startup method.

Using these register methods ensures that script code gets written to
the page only once. You would not have this protection with the literal
method of writing the script to the page.

Another common gotcha when client scripting is that if <asp:<elements>
have the visible property set to false, they are not rendered on the
html page and as such can not be referenced from script code. A
work-arround is to leave the visible property set to true and then add
the attribute("style", "display:none;").

As always, learnasp.com is an excellent source of reference with links
to many asp.net related topics.

 
Didn't find what you were looking for? Find more on Problem with Focusing the Text box on Page Load Or get search suggestion and latest updates.




Tagged: