Home > Archive > ASP .NET > February 2005 > Giving Focus on PostBack
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Giving Focus on PostBack
|
|
|
| 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
| |
| Andy G 2005-02-15, 9:02 pm |
| 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
"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:eczSz76EFHA.1260@TK2MSFTNGP12.phx.gbl...
> 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
>
>
| |
|
| "Andy G" <ajgould@iastate.edu> wrote in message
news:Onga0C7EFHA.3376@TK2MSFTNGP12.phx.gbl...
>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" <tscheiderich@ftsolutions.com> wrote in message
> news:eczSz76EFHA.1260@TK2MSFTNGP12.phx.gbl...
> to
> repost?
>
>
| |
|
| 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" <tscheiderich@ftsolutions.com> wrote in message
news:ucX1rK7EFHA.3984@TK2MSFTNGP14.phx.gbl...
> "Andy G" <ajgould@iastate.edu> wrote in message
> news:Onga0C7EFHA.3376@TK2MSFTNGP12.phx.gbl...
>
> 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
of[color=darkred]
way[color=darkred]
>
>
| |
|
| "Shawn" <bossman100@hotmail.com> wrote in message
news:el1UMb7EFHA.3120@TK2MSFTNGP12.phx.gbl...
> 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" <tscheiderich@ftsolutions.com> wrote in message
> news:ucX1rK7EFHA.3984@TK2MSFTNGP14.phx.gbl...
> of
> way
>
>
| |
|
|
"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:e44YnKGFFHA.2600@TK2MSFTNGP09.phx.gbl...
> "Shawn" <bossman100@hotmail.com> wrote in message
> news:el1UMb7EFHA.3120@TK2MSFTNGP12.phx.gbl...
>
> 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
| |
|
| 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" <tscheiderich@ftsolutions.com> wrote in message
news:%23LTHpTGFFHA.3284@TK2MSFTNGP09.phx.gbl...
>
> "tshad" <tscheiderich@ftsolutions.com> wrote in message
> news:e44YnKGFFHA.2600@TK2MSFTNGP09.phx.gbl...
>
> 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
>
| |
|
| Well, you can just clear it, like this: myBody.Attributes.Item("onLoad") =
""
Shawn
"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:edopD9TFFHA.1264@TK2MSFTNGP12.phx.gbl...
> 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" <tscheiderich@ftsolutions.com> wrote in message
> news:%23LTHpTGFFHA.3284@TK2MSFTNGP09.phx.gbl...
</body>[color=darkred]
so).[color=darkred]
a[color=darkred]
>
>
| |
|
| "Shawn" <bossman100@hotmail.com> wrote in message
news:upw8G$ZFFHA.1932@TK2MSFTNGP14.phx.gbl...
> 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" <tscheiderich@ftsolutions.com> wrote in message
> news:edopD9TFFHA.1264@TK2MSFTNGP12.phx.gbl...
> </body>
> so).
> a
>
>
| |
|
| "tshad" <tscheiderich@ftsolutions.com> wrote in message
news:OhYFrReFFHA.3336@TK2MSFTNGP10.phx.gbl...
> "Shawn" <bossman100@hotmail.com> wrote in message
> news:upw8G$ZFFHA.1932@TK2MSFTNGP14.phx.gbl...
>
> 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
>
>
| |
| Joshua Flanagan 2005-02-28, 3:59 am |
| 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
tshad wrote:
> "tshad" <tscheiderich@ftsolutions.com> wrote in message
> news:OhYFrReFFHA.3336@TK2MSFTNGP10.phx.gbl...
>
>
>
> I figured it out.
>
> I just needed to add:
>
> myBody.Attributes.Item("onLoad") =
> "document.forms[0].ticklerPhrase.focus()"
>
> to my IsPostBack section.
>
> Thanks,
>
> Tom
>
>
>
|
|
|
|
|