Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Setting Focus on an asp:textbox

Reply
Thread Tools

Setting Focus on an asp:textbox

 
 
George Durzi
Guest
Posts: n/a
 
      08-20-2003
I'm trying to set focus on an asp:textbox. I'm running this code after my
page_load

StringBuilder sb = new StringBuilder();

sb.Append(@"<script language=javascript>");
sb.Append(@"<!--");
sb.Append(@"document.getElementById(txtUserName).f ocus();");
sb.Append(@"-->");
sb.Append(@"</script>");

if (!IsStartupScriptRegistered("LoginFocus"))
RegisterStartupScript("LoginFocus", sb.ToString());

If I View Source, I can confirm that the JavaScript is being outputed before
the </form> tag, but my control is not getting focus ...

Any idea?


 
Reply With Quote
 
 
 
 
George Durzi
Guest
Posts: n/a
 
      08-20-2003
As far as my understanding of RegisterStartupScript goes, this script is
rendered as the last thing before the </form> tag, so it's rendered AFTER
the text box was rendered.


"Marina" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> That is because your script is running before the textbox has been

rendered.
> Make this scrip the onload handler for the window object.
>
> "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
> news:(E-Mail Removed)...
> > I'm trying to set focus on an asp:textbox. I'm running this code after

my
> > page_load
> >
> > StringBuilder sb = new StringBuilder();
> >
> > sb.Append(@"<script language=javascript>");
> > sb.Append(@"<!--");
> > sb.Append(@"document.getElementById(txtUserName).f ocus();");
> > sb.Append(@"-->");
> > sb.Append(@"</script>");
> >
> > if (!IsStartupScriptRegistered("LoginFocus"))
> > RegisterStartupScript("LoginFocus", sb.ToString());
> >
> > If I View Source, I can confirm that the JavaScript is being outputed

> before
> > the </form> tag, but my control is not getting focus ...
> >
> > Any idea?
> >
> >

>
>



 
Reply With Quote
 
 
 
 
George Durzi
Guest
Posts: n/a
 
      08-20-2003
Yes, and it's rendering at the end as defined by the RegisterStartupScript
method. In my first message, I posted
> > > > If I View Source, I can confirm that the JavaScript is being

outputed
> > > before
> > > > the </form> tag, but my control is not getting focus ...


"Marina" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I believe it is rendered in the beginning - not end.
>
> The best way to check would be to look at the rendered HTML source -
> wouldn't it?
>
> "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
> news:(E-Mail Removed)...
> > As far as my understanding of RegisterStartupScript goes, this script is
> > rendered as the last thing before the </form> tag, so it's rendered

AFTER
> > the text box was rendered.
> >
> >
> > "Marina" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> > > That is because your script is running before the textbox has been

> > rendered.
> > > Make this scrip the onload handler for the window object.
> > >
> > > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
> > > news:(E-Mail Removed)...
> > > > I'm trying to set focus on an asp:textbox. I'm running this code

after
> > my
> > > > page_load
> > > >
> > > > StringBuilder sb = new StringBuilder();
> > > >
> > > > sb.Append(@"<script language=javascript>");
> > > > sb.Append(@"<!--");
> > > > sb.Append(@"document.getElementById(txtUserName).f ocus();");
> > > > sb.Append(@"-->");
> > > > sb.Append(@"</script>");
> > > >
> > > > if (!IsStartupScriptRegistered("LoginFocus"))
> > > > RegisterStartupScript("LoginFocus", sb.ToString());
> > > >


> > > >
> > > > Any idea?
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
George Durzi
Guest
Posts: n/a
 
      08-20-2003
It worked. Very strange. the only think I changed was removing
sb.Append(@"<!--");
and
sb.Append(@"-->");

"George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
news:%(E-Mail Removed)...
> Yes, and it's rendering at the end as defined by the RegisterStartupScript
> method. In my first message, I posted
> > > > > If I View Source, I can confirm that the JavaScript is being

> outputed
> > > > before
> > > > > the </form> tag, but my control is not getting focus ...

>
> "Marina" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I believe it is rendered in the beginning - not end.
> >
> > The best way to check would be to look at the rendered HTML source -
> > wouldn't it?
> >
> > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
> > news:(E-Mail Removed)...
> > > As far as my understanding of RegisterStartupScript goes, this script

is
> > > rendered as the last thing before the </form> tag, so it's rendered

> AFTER
> > > the text box was rendered.
> > >
> > >
> > > "Marina" <(E-Mail Removed)> wrote in message
> > > news:%(E-Mail Removed)...
> > > > That is because your script is running before the textbox has been
> > > rendered.
> > > > Make this scrip the onload handler for the window object.
> > > >
> > > > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
> > > > news:(E-Mail Removed)...
> > > > > I'm trying to set focus on an asp:textbox. I'm running this code

> after
> > > my
> > > > > page_load
> > > > >
> > > > > StringBuilder sb = new StringBuilder();
> > > > >
> > > > > sb.Append(@"<script language=javascript>");
> > > > > sb.Append(@"<!--");
> > > > > sb.Append(@"document.getElementById(txtUserName).f ocus();");
> > > > > sb.Append(@"-->");
> > > > > sb.Append(@"</script>");
> > > > >
> > > > > if (!IsStartupScriptRegistered("LoginFocus"))
> > > > > RegisterStartupScript("LoginFocus", sb.ToString());
> > > > >

>
> > > > >
> > > > > Any idea?
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Marina
Guest
Posts: n/a
 
      08-20-2003
Ah, sorry, was thinking of the RegisterClientScriptBlock method.

"George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
news:%(E-Mail Removed)...
> Yes, and it's rendering at the end as defined by the RegisterStartupScript
> method. In my first message, I posted
> > > > > If I View Source, I can confirm that the JavaScript is being

> outputed
> > > > before
> > > > > the </form> tag, but my control is not getting focus ...

>
> "Marina" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I believe it is rendered in the beginning - not end.
> >
> > The best way to check would be to look at the rendered HTML source -
> > wouldn't it?
> >
> > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
> > news:(E-Mail Removed)...
> > > As far as my understanding of RegisterStartupScript goes, this script

is
> > > rendered as the last thing before the </form> tag, so it's rendered

> AFTER
> > > the text box was rendered.
> > >
> > >
> > > "Marina" <(E-Mail Removed)> wrote in message
> > > news:%(E-Mail Removed)...
> > > > That is because your script is running before the textbox has been
> > > rendered.
> > > > Make this scrip the onload handler for the window object.
> > > >
> > > > "George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
> > > > news:(E-Mail Removed)...
> > > > > I'm trying to set focus on an asp:textbox. I'm running this code

> after
> > > my
> > > > > page_load
> > > > >
> > > > > StringBuilder sb = new StringBuilder();
> > > > >
> > > > > sb.Append(@"<script language=javascript>");
> > > > > sb.Append(@"<!--");
> > > > > sb.Append(@"document.getElementById(txtUserName).f ocus();");
> > > > > sb.Append(@"-->");
> > > > > sb.Append(@"</script>");
> > > > >
> > > > > if (!IsStartupScriptRegistered("LoginFocus"))
> > > > > RegisterStartupScript("LoginFocus", sb.ToString());
> > > > >

>
> > > > >
> > > > > Any idea?
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
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
emulating window.focus in Body onload() event and setting focus to a control on same page Jason ASP .Net 4 05-07-2007 05:54 PM
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
Fuji EXIF: Focus warning: "Out of focus" Hasan Diwan Digital Photography 3 12-26-2004 04:13 AM
Pan Focus vs Auto Focus JethroUK© Digital Photography 2 08-11-2004 06:26 PM
any manual focus cameras easy to focus? Catherine Jo Morgan Digital Photography 6 11-05-2003 06:36 PM



Advertisments