Giving Focus on PostBack

T

tshad

I have a page where I give focus to the first textbox on my page:

<body onLoad="document.forms[0].firstName.focus();">

I also go and check the status of some data when I exit the 3rd textbox on
my screen.

The problem is that the screen jumps back to the first textbox because of
the onLoad statement when the page is posted back. Is there an easy way to
tell it to do the Onload only once and to go the next textbox on the repost?

Thanks,

Tom
 
A

Andy G

I think this is what you want....

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

<body onLoad="document.forms[0].firstName.focus();">

End If

End Sub
 
T

tshad

Andy G said:
I think this is what you want....

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

<body onLoad="document.forms[0].firstName.focus();">

End If

End Sub

I tried that but got the error:

Compiler Error Message: BC30636: '>' expected.

My code is:

Sub Page_Load(sender as Object, e as EventArgs)
if not IsPostBack then
<body onLoad="document.forms[0].firstName.focus();">
end if
end sub

Tom
tshad said:
I have a page where I give focus to the first textbox on my page:

<body onLoad="document.forms[0].firstName.focus();">

I also go and check the status of some data when I exit the 3rd textbox
on
my screen.

The problem is that the screen jumps back to the first textbox because of
the onLoad statement when the page is posted back. Is there an easy way to
tell it to do the Onload only once and to go the next textbox on the repost?

Thanks,

Tom
 
S

Shawn

Hi.
Add a Runat="server" and id="myBody" to the body tag in the aspx file. Then
put this in your code behind:
Protected WithEvents myBody As System.Web.UI.HtmlControls.HtmlGenericControl

Now you can add the javascript in you page load method, like this:
If Not IsPostBack Then
myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")
End If

Shawn

tshad said:
Andy G said:
I think this is what you want....

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

<body onLoad="document.forms[0].firstName.focus();">

End If

End Sub

I tried that but got the error:

Compiler Error Message: BC30636: '>' expected.

My code is:

Sub Page_Load(sender as Object, e as EventArgs)
if not IsPostBack then
<body onLoad="document.forms[0].firstName.focus();">
end if
end sub

Tom
tshad said:
I have a page where I give focus to the first textbox on my page:

<body onLoad="document.forms[0].firstName.focus();">

I also go and check the status of some data when I exit the 3rd textbox
on
my screen.

The problem is that the screen jumps back to the first textbox because of
the onLoad statement when the page is posted back. Is there an easy
way
to
tell it to do the Onload only once and to go the next textbox on the repost?

Thanks,

Tom
 
T

tshad

Shawn said:
Hi.
Add a Runat="server" and id="myBody" to the body tag in the aspx file.
Then
put this in your code behind:
Protected WithEvents myBody As
System.Web.UI.HtmlControls.HtmlGenericControl

Now you can add the javascript in you page load method, like this:
If Not IsPostBack Then
myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")
End If

I tried that and got the following error (even though there is a </body> at
the bottom of the page):
********************************************************
Parser Error Message: Unexpected end of file looking for </body> tag.

Source Error:

Line 433:</head>
Line 434:<link href="staffing.css" rel="stylesheet" type="text/css">
Line 435:<body id="myBody" runat="server">
Line 436:<fts:header id=ctl1 runat="Server" />
*******************************************************
Do I need to do something else (need to body tags - I wouldn't think so).

Tom
Shawn

tshad said:
Andy G said:
I think this is what you want....

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

<body onLoad="document.forms[0].firstName.focus();">

End If

End Sub

I tried that but got the error:

Compiler Error Message: BC30636: '>' expected.

My code is:

Sub Page_Load(sender as Object, e as EventArgs)
if not IsPostBack then
<body onLoad="document.forms[0].firstName.focus();">
end if
end sub

Tom
I have a page where I give focus to the first textbox on my page:

<body onLoad="document.forms[0].firstName.focus();">

I also go and check the status of some data when I exit the 3rd
textbox
on
my screen.

The problem is that the screen jumps back to the first textbox because of
the onLoad statement when the page is posted back. Is there an easy way
to
tell it to do the Onload only once and to go the next textbox on the
repost?

Thanks,

Tom
 
T

tshad

tshad said:
Shawn said:
Hi.
Add a Runat="server" and id="myBody" to the body tag in the aspx file.
Then
put this in your code behind:
Protected WithEvents myBody As
System.Web.UI.HtmlControls.HtmlGenericControl

Now you can add the javascript in you page load method, like this:
If Not IsPostBack Then
myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")
End If

I tried that and got the following error (even though there is a </body>
at the bottom of the page):
********************************************************
Parser Error Message: Unexpected end of file looking for </body> tag.

Source Error:

Line 433:</head>
Line 434:<link href="staffing.css" rel="stylesheet" type="text/css">
Line 435:<body id="myBody" runat="server">
Line 436:<fts:header id=ctl1 runat="Server" />
*******************************************************
Do I need to do something else (need to body tags - I wouldn't think so).

I found out what was causing this error, although it makes user controls a
bit of a problem

At the bottom of my page I have:

<fts:footer id=ctl2 runat="Server" />

This has the following code in it:

</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

If I take out the control and just put this code it by hand, I don't get the
error.

Also, This doesn't seem to work. Even though I am adding the the "onload"
to the body attribute, it is still there when the page is re-posted, so it
always goes back to the first textbox.

Tom
 
T

tshad

I was just thinking:

If you do:

myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")

to add the attribute during Page_Load, is there a corresponding:

myBody.Attributes.remove (or something like that take it out).

This then allow me to take if off during the repost, which is what is
causing my problem.

Thanks,

Tom



tshad said:
tshad said:
Shawn said:
Hi.
Add a Runat="server" and id="myBody" to the body tag in the aspx file.
Then
put this in your code behind:
Protected WithEvents myBody As
System.Web.UI.HtmlControls.HtmlGenericControl

Now you can add the javascript in you page load method, like this:
If Not IsPostBack Then
myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")
End If

I tried that and got the following error (even though there is a </body>
at the bottom of the page):
********************************************************
Parser Error Message: Unexpected end of file looking for </body> tag.

Source Error:

Line 433:</head>
Line 434:<link href="staffing.css" rel="stylesheet" type="text/css">
Line 435:<body id="myBody" runat="server">
Line 436:<fts:header id=ctl1 runat="Server" />
*******************************************************
Do I need to do something else (need to body tags - I wouldn't think so).

I found out what was causing this error, although it makes user controls a
bit of a problem

At the bottom of my page I have:

<fts:footer id=ctl2 runat="Server" />

This has the following code in it:

</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

If I take out the control and just put this code it by hand, I don't get
the error.

Also, This doesn't seem to work. Even though I am adding the the
"onload" to the body attribute, it is still there when the page is
re-posted, so it always goes back to the first textbox.

Tom
 
S

Shawn

Well, you can just clear it, like this: myBody.Attributes.Item("onLoad") =
""
Shawn

tshad said:
I was just thinking:

If you do:

myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")

to add the attribute during Page_Load, is there a corresponding:

myBody.Attributes.remove (or something like that take it out).

This then allow me to take if off during the repost, which is what is
causing my problem.

Thanks,

Tom



tshad said:
tshad said:
Hi.
Add a Runat="server" and id="myBody" to the body tag in the aspx file.
Then
put this in your code behind:
Protected WithEvents myBody As
System.Web.UI.HtmlControls.HtmlGenericControl

Now you can add the javascript in you page load method, like this:
If Not IsPostBack Then
myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")
End If

I tried that and got the following error (even though there is a
at the bottom of the page):
********************************************************
Parser Error Message: Unexpected end of file looking for </body> tag.

Source Error:

Line 433:</head>
Line 434:<link href="staffing.css" rel="stylesheet" type="text/css">
Line 435:<body id="myBody" runat="server">
Line 436:<fts:header id=ctl1 runat="Server" />
*******************************************************
Do I need to do something else (need to body tags - I wouldn't think so).

I found out what was causing this error, although it makes user controls a
bit of a problem

At the bottom of my page I have:

<fts:footer id=ctl2 runat="Server" />

This has the following code in it:

</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

If I take out the control and just put this code it by hand, I don't get
the error.

Also, This doesn't seem to work. Even though I am adding the the
"onload" to the body attribute, it is still there when the page is
re-posted, so it always goes back to the first textbox.

Tom
 
T

tshad

Shawn said:
Well, you can just clear it, like this: myBody.Attributes.Item("onLoad") =
""

That was what I was looking for, but it didn't do what I wanted.

When I post back, it goes to the first link at the top of the page.

What I am doing is this:

I have 10 textboxes. When I enter the page the first time, I give focus to
the first text box. After the 3 textbox, I go check to see if what was
entered was valid and if valid, I fill the rest of the text boxes. I then
want it to go to the next text box. It goes to the first textbox when it
comes back (the old way) and to the top of the page (the second way when I
set onload to "").

It makes sense as to why it does this. But is there a way to know where it
was on exit (which would be the third box) and then go to the 4th text box
on reentry?

Thanks,

Tom
Shawn

tshad said:
I was just thinking:

If you do:

myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")

to add the attribute during Page_Load, is there a corresponding:

myBody.Attributes.remove (or something like that take it out).

This then allow me to take if off during the repost, which is what is
causing my problem.

Thanks,

Tom



tshad said:
Hi.
Add a Runat="server" and id="myBody" to the body tag in the aspx
file.
Then
put this in your code behind:
Protected WithEvents myBody As
System.Web.UI.HtmlControls.HtmlGenericControl

Now you can add the javascript in you page load method, like this:
If Not IsPostBack Then
myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")
End If

I tried that and got the following error (even though there is a
at the bottom of the page):
********************************************************
Parser Error Message: Unexpected end of file looking for </body> tag.

Source Error:

Line 433:</head>
Line 434:<link href="staffing.css" rel="stylesheet" type="text/css">
Line 435:<body id="myBody" runat="server">
Line 436:<fts:header id=ctl1 runat="Server" />
*******************************************************
Do I need to do something else (need to body tags - I wouldn't think so).


I found out what was causing this error, although it makes user
controls a
bit of a problem

At the bottom of my page I have:

<fts:footer id=ctl2 runat="Server" />

This has the following code in it:

</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

If I take out the control and just put this code it by hand, I don't
get
the error.

Also, This doesn't seem to work. Even though I am adding the the
"onload" to the body attribute, it is still there when the page is
re-posted, so it always goes back to the first textbox.

Tom
 
T

tshad

tshad said:
That was what I was looking for, but it didn't do what I wanted.

When I post back, it goes to the first link at the top of the page.

What I am doing is this:

I have 10 textboxes. When I enter the page the first time, I give focus
to the first text box. After the 3 textbox, I go check to see if what was
entered was valid and if valid, I fill the rest of the text boxes. I then
want it to go to the next text box. It goes to the first textbox when it
comes back (the old way) and to the top of the page (the second way when I
set onload to "").

It makes sense as to why it does this. But is there a way to know where
it was on exit (which would be the third box) and then go to the 4th text
box on reentry?

I figured it out.

I just needed to add:

myBody.Attributes.Item("onLoad") =
"document.forms[0].ticklerPhrase.focus()"

to my IsPostBack section.

Thanks,

Tom
Thanks,

Tom
Shawn

tshad said:
I was just thinking:

If you do:

myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")

to add the attribute during Page_Load, is there a corresponding:

myBody.Attributes.remove (or something like that take it out).

This then allow me to take if off during the repost, which is what is
causing my problem.

Thanks,

Tom




Hi.
Add a Runat="server" and id="myBody" to the body tag in the aspx
file.
Then
put this in your code behind:
Protected WithEvents myBody As
System.Web.UI.HtmlControls.HtmlGenericControl

Now you can add the javascript in you page load method, like this:
If Not IsPostBack Then
myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")
End If

I tried that and got the following error (even though there is a
at the bottom of the page):
********************************************************
Parser Error Message: Unexpected end of file looking for </body> tag.

Source Error:

Line 433:</head>
Line 434:<link href="staffing.css" rel="stylesheet" type="text/css">
Line 435:<body id="myBody" runat="server">
Line 436:<fts:header id=ctl1 runat="Server" />
*******************************************************
Do I need to do something else (need to body tags - I wouldn't think so).


I found out what was causing this error, although it makes user
controls a
bit of a problem

At the bottom of my page I have:

<fts:footer id=ctl2 runat="Server" />

This has the following code in it:

</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

If I take out the control and just put this code it by hand, I don't
get
the error.

Also, This doesn't seem to work. Even though I am adding the the
"onload" to the body attribute, it is still there when the page is
re-posted, so it always goes back to the first textbox.

Tom
 
J

Joshua Flanagan

Scott Guthrie demonstrates a great reusable way to get this functionality.

Look at the ASP.NET BlackBelt WebForms talk in the TechEd 2003
Presentations section.
http://www.scottgu.com/

Joshua Flanagan
http://flimflan.com/blog


That was what I was looking for, but it didn't do what I wanted.

When I post back, it goes to the first link at the top of the page.

What I am doing is this:

I have 10 textboxes. When I enter the page the first time, I give focus way
entered was valid and if valid, I fill the rest of the text boxes. I then
want it to go to the next text box. It goes to the first textbox when it
comes back (the old way) and to the top of the page (the second way when I
set onload to "").

It makes sense as to why it does this. But is there a way to know where
it was on exit (which would be the third box) and then go to the 4th text
box on reentry?


I figured it out.

I just needed to add:

myBody.Attributes.Item("onLoad") =
"document.forms[0].ticklerPhrase.focus()"

to my IsPostBack section.

Thanks,

Tom
Thanks,

Tom
Shawn


I was just thinking:

If you do:

myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")

to add the attribute during Page_Load, is there a corresponding:

myBody.Attributes.remove (or something like that take it out).

This then allow me to take if off during the repost, which is what is
causing my problem.

Thanks,

Tom






Hi.
Add a Runat="server" and id="myBody" to the body tag in the aspx
file.
Then
put this in your code behind:
Protected WithEvents myBody As
System.Web.UI.HtmlControls.HtmlGenericControl

Now you can add the javascript in you page load method, like this:
If Not IsPostBack Then
myBody.Attributes.Add("onLoad",
"document.forms[0].the_id_of_the_first_textbox.focus();")
End If

I tried that and got the following error (even though there is a

</body>

at the bottom of the page):
********************************************************
Parser Error Message: Unexpected end of file looking for </body> tag.

Source Error:

Line 433:</head>
Line 434:<link href="staffing.css" rel="stylesheet" type="text/css">
Line 435:<body id="myBody" runat="server">
Line 436:<fts:header id=ctl1 runat="Server" />
*******************************************************
Do I need to do something else (need to body tags - I wouldn't think

so).

I found out what was causing this error, although it makes user
controls

a

bit of a problem

At the bottom of my page I have:

<fts:footer id=ctl2 runat="Server" />

This has the following code in it:

</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

If I take out the control and just put this code it by hand, I don't
get
the error.

Also, This doesn't seem to work. Even though I am adding the the
"onload" to the body attribute, it is still there when the page is
re-posted, so it always goes back to the first textbox.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top