Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Giving Focus on PostBack

Reply
Thread Tools

Giving Focus on PostBack

 
 
tshad
Guest
Posts: n/a
 
      02-15-2005
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


 
Reply With Quote
 
 
 
 
Andy G
Guest
Posts: n/a
 
      02-15-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
tshad
Guest
Posts: n/a
 
      02-15-2005
"Andy G" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>

>
>



 
Reply With Quote
 
Shawn
Guest
Posts: n/a
 
      02-15-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Andy G" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> 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
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      02-16-2005
"Shawn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "Andy G" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >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" <(E-Mail Removed)> wrote in message
>> > news:(E-Mail Removed)...
>> >> 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
>> >>
>> >>
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      02-16-2005

"tshad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Shawn" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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


 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      02-17-2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> "tshad" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "Shawn" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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
>



 
Reply With Quote
 
Shawn
Guest
Posts: n/a
 
      02-18-2005
Well, you can just clear it, like this: myBody.Attributes.Item("onLoad") =
""
Shawn

"tshad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> >
> > "tshad" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> "Shawn" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >>> 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
> >

>
>



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      02-18-2005
"Shawn" <(E-Mail Removed)> wrote in message
news:upw8G$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>> >
>> > "tshad" <(E-Mail Removed)> wrote in message
>> > news:(E-Mail Removed)...
>> >> "Shawn" <(E-Mail Removed)> wrote in message
>> >> news:(E-Mail Removed)...
>> >>> 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
>> >

>>
>>

>
>



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      02-18-2005
"tshad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Shawn" <(E-Mail Removed)> wrote in message
> news:upw8G$(E-Mail Removed)...
>> 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?


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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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" <(E-Mail Removed)> wrote in message
>>> news:%(E-Mail Removed)...
>>> >
>>> > "tshad" <(E-Mail Removed)> wrote in message
>>> > news:(E-Mail Removed)...
>>> >> "Shawn" <(E-Mail Removed)> wrote in message
>>> >> news:(E-Mail Removed)...
>>> >>> 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
>>> >
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The giving that keeps on giving sixteenmillion C Programming 0 11-19-2007 10:59 PM
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
Giving Focus to the contents of an IFrame Mike ASP .Net 1 05-30-2006 12:08 PM
Displaying a JDialog without giving it focus Scott Steiner Java 0 05-09-2005 08:13 PM
giving form input bos focus new Karoo HTML 4 07-26-2003 07:52 AM



Advertisments