473,385 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Setting Focus on an asp:textbox

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?
Nov 17 '05 #1
4 1512
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" <zl*******@nospam.hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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:OV**************@TK2MSFTNGP12.phx.gbl...
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?


Nov 17 '05 #2
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" <zl*******@nospam.hotmail.com> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl...
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:uA**************@TK2MSFTNGP09.phx.gbl...
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" <zl*******@nospam.hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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:OV**************@TK2MSFTNGP12.phx.gbl...
> 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?
>
>



Nov 17 '05 #3
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:%2***************@TK2MSFTNGP10.phx.gbl...
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" <zl*******@nospam.hotmail.com> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl...
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:uA**************@TK2MSFTNGP09.phx.gbl...
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" <zl*******@nospam.hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> 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:OV**************@TK2MSFTNGP12.phx.gbl...
> > 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?
> >
> >
>
>



Nov 17 '05 #4
Ah, sorry, was thinking of the RegisterClientScriptBlock method.

"George Durzi" <gdurzi@nospam_hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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" <zl*******@nospam.hotmail.com> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl...
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:uA**************@TK2MSFTNGP09.phx.gbl...
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" <zl*******@nospam.hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> 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:OV**************@TK2MSFTNGP12.phx.gbl...
> > 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?
> >
> >
>
>



Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sarj | last post by:
Hi.... I have a asp:textbox whose text needs to be set when a radio button is clicked. The text would correspond to the value of the particular. However, although the text is correctly set in...
1
by: Shilpi Chaudhry | last post by:
I have a forloop which I use to create textboxes - but unfortunately all of them have the same ids - I need to assign different ids to these textboxes creeated in the loop so that I can retrieve...
2
by: R.A.M. | last post by:
Hello, Could you help me plase? I need to set Text property of TextBox when second page is displayed and active. On first page I have: Birth date: <asp:TextBox ID="BirthDate" runat="server"...
1
by: R.A.M. | last post by:
Hi, I need to set BirtDate TextBox with a date taken from another browser window with Calendar control. I tried this way: On first page I have: Birth date: <asp:TextBox ID="BirthDate"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.