How To? - Use Javascript To Test If a TextBox Exists On a Page

Discussion in 'ASP .Net' started by joey.powell@topscene.com, Jul 14, 2005.

  1. Guest

    I have a home page with username and password textboxes and a login
    button for purposes of users being able to log in (forms
    authentication) directly on the site home page. I also have a dedicated
    "Login.aspx" page, but I don't want users redirected there unless
    absolutely necessary.

    I have registered a javascript block via "Page.RegisterStartupScript"
    that sets focus to the "txtUserName" textbox control on Page Load. It
    works great for IE and Firefox.

    But...

    The problem is that the "txtUserName" textbox does not exist once the
    user has logged in and the page posts back. The code behind then knows
    that the user is authenticated, and so it does a ".Visible=false" for
    the "txtUserName", "txtPassword" and "btnLogin" controls.

    Then, when the page loads after the postback, the javascript block
    causes an error icon in the lower left corner of the browser.
    Apparently it is still trying to set focus to the control? I thought I
    had this fixed when I set the code behind to register the page startup
    script only when the page loaded for the first time...but that didn't
    work for some reason, because it still tries to set focus on postbacks.
    I also tried disabling the page viewstate along with the above, but
    that didn't work either.

    Can the javascript block be coded to test if the "txtUserName"
    (document.txtUserName ?) textbox exists before trying to set focus to
    it? If so then that would do it...but how would the code look? Also, is
    there another solution that I am not seeing here? I need to get this
    working correctly.

    Any suggestions are appreciated. Thanks.
    , Jul 14, 2005
    #1
    1. Advertising

  2. I am not sure what the actual problem is. I understand what you are trying to
    do, but not the why you are trying to do it.

    Is the problem, there are certain pages that a user should not have to log
    in to use? If so, restrict those pages by turning authentication off for
    those pages. You set up a separate section of the web.config to exclude those
    pages. Conversely, you can restrict the small number of login pages, if they
    are fewer.

    Is the problem, depending on how a user gets into the app they may be
    authenticated by the network. If so, set up the login cookie when you find
    those types of users and they will never be redirected to the login.aspx page.

    What am I getting at? You are asking how to do something that is rather
    kludgy and it is more likely you are doing this out of a misunderstanding of
    how ASP.NET authentication works rather than a real need to do it this way.
    By rephrasing the why, you might find that you do not have to go to the
    trouble of adding JavaScript to the login page to hide controls from the user.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    ***************************
    Think Outside the Box!
    ***************************


    "" wrote:

    > I have a home page with username and password textboxes and a login
    > button for purposes of users being able to log in (forms
    > authentication) directly on the site home page. I also have a dedicated
    > "Login.aspx" page, but I don't want users redirected there unless
    > absolutely necessary.
    >
    > I have registered a javascript block via "Page.RegisterStartupScript"
    > that sets focus to the "txtUserName" textbox control on Page Load. It
    > works great for IE and Firefox.
    >
    > But...
    >
    > The problem is that the "txtUserName" textbox does not exist once the
    > user has logged in and the page posts back. The code behind then knows
    > that the user is authenticated, and so it does a ".Visible=false" for
    > the "txtUserName", "txtPassword" and "btnLogin" controls.
    >
    > Then, when the page loads after the postback, the javascript block
    > causes an error icon in the lower left corner of the browser.
    > Apparently it is still trying to set focus to the control? I thought I
    > had this fixed when I set the code behind to register the page startup
    > script only when the page loaded for the first time...but that didn't
    > work for some reason, because it still tries to set focus on postbacks.
    > I also tried disabling the page viewstate along with the above, but
    > that didn't work either.
    >
    > Can the javascript block be coded to test if the "txtUserName"
    > (document.txtUserName ?) textbox exists before trying to set focus to
    > it? If so then that would do it...but how would the code look? Also, is
    > there another solution that I am not seeing here? I need to get this
    > working correctly.
    >
    > Any suggestions are appreciated. Thanks.
    >
    >
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN, Jul 14, 2005
    #2
    1. Advertising

  3. Guest

    Gregory, I think it is you that does not understand. While I know that
    I have a lack of understanding about some things in asp.net (like
    everyone else?), I am fairly confident that I do not have a
    misunderstanding! :)

    In my original post I never said anything about using javascript to
    hide controls...that would not make any sense here. Maybe I just worded
    it in a confusing way...if so then I am sorry.

    I am only using javascript to set focus to my txtUserName textbox. The
    hiding of controls is occuring in the code behind once the user logs
    in, as it should. The problem is that the javascript continues to try
    to set focus once the page does a post back and the controls are hidden
    (null). This generates the error. I simply wanted a javascript sample
    that could be used to test for the controls on the page before
    attempting to set focus.

    I think that Eliyahu's post above will do just that.

    Anyways, thanks a bunch to both of you!

    JP>
    , Jul 14, 2005
    #3
  4. var txtBox=document,getElementById( "txtUserName");
    if (txtBox!=null)
    txtBox.focus();


    Eliyahu

    <> wrote in message
    news:...
    > I have a home page with username and password textboxes and a login
    > button for purposes of users being able to log in (forms
    > authentication) directly on the site home page. I also have a dedicated
    > "Login.aspx" page, but I don't want users redirected there unless
    > absolutely necessary.
    >
    > I have registered a javascript block via "Page.RegisterStartupScript"
    > that sets focus to the "txtUserName" textbox control on Page Load. It
    > works great for IE and Firefox.
    >
    > But...
    >
    > The problem is that the "txtUserName" textbox does not exist once the
    > user has logged in and the page posts back. The code behind then knows
    > that the user is authenticated, and so it does a ".Visible=false" for
    > the "txtUserName", "txtPassword" and "btnLogin" controls.
    >
    > Then, when the page loads after the postback, the javascript block
    > causes an error icon in the lower left corner of the browser.
    > Apparently it is still trying to set focus to the control? I thought I
    > had this fixed when I set the code behind to register the page startup
    > script only when the page loaded for the first time...but that didn't
    > work for some reason, because it still tries to set focus on postbacks.
    > I also tried disabling the page viewstate along with the above, but
    > that didn't work either.
    >
    > Can the javascript block be coded to test if the "txtUserName"
    > (document.txtUserName ?) textbox exists before trying to set focus to
    > it? If so then that would do it...but how would the code look? Also, is
    > there another solution that I am not seeing here? I need to get this
    > working correctly.
    >
    > Any suggestions are appreciated. Thanks.
    >
    Eliyahu Goldin, Jul 14, 2005
    #4
  5. You can also do

    if(typeof(txtUserName) != "undefined")
    txtBox.focus();

    Sreejith

    "Eliyahu Goldin" wrote:

    > var txtBox=document,getElementById( "txtUserName");
    > if (txtBox!=null)
    > txtBox.focus();
    >
    >
    > Eliyahu
    >
    > <> wrote in message
    > news:...
    > > I have a home page with username and password textboxes and a login
    > > button for purposes of users being able to log in (forms
    > > authentication) directly on the site home page. I also have a dedicated
    > > "Login.aspx" page, but I don't want users redirected there unless
    > > absolutely necessary.
    > >
    > > I have registered a javascript block via "Page.RegisterStartupScript"
    > > that sets focus to the "txtUserName" textbox control on Page Load. It
    > > works great for IE and Firefox.
    > >
    > > But...
    > >
    > > The problem is that the "txtUserName" textbox does not exist once the
    > > user has logged in and the page posts back. The code behind then knows
    > > that the user is authenticated, and so it does a ".Visible=false" for
    > > the "txtUserName", "txtPassword" and "btnLogin" controls.
    > >
    > > Then, when the page loads after the postback, the javascript block
    > > causes an error icon in the lower left corner of the browser.
    > > Apparently it is still trying to set focus to the control? I thought I
    > > had this fixed when I set the code behind to register the page startup
    > > script only when the page loaded for the first time...but that didn't
    > > work for some reason, because it still tries to set focus on postbacks.
    > > I also tried disabling the page viewstate along with the above, but
    > > that didn't work either.
    > >
    > > Can the javascript block be coded to test if the "txtUserName"
    > > (document.txtUserName ?) textbox exists before trying to set focus to
    > > it? If so then that would do it...but how would the code look? Also, is
    > > there another solution that I am not seeing here? I need to get this
    > > working correctly.
    > >
    > > Any suggestions are appreciated. Thanks.
    > >

    >
    >
    >
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Jul 14, 2005
    #5
  6. Ideally, you should remove the scripts that are not needed for the current
    execution of the page.

    On page_load, if the request is a not a postback, attach the scripts to the
    startup for the textbox focus. If the request is a postback, then don't
    attach the scripts.
    --
    Direct Email: Michael.Baltic@RemoveCharactersUpTo#NCMC.Com

    Staff Consultant II
    Enterprise Web Services
    Cardinal Solutions Group


    "Sreejith Ram" wrote:

    > You can also do
    >
    > if(typeof(txtUserName) != "undefined")
    > txtBox.focus();
    >
    > Sreejith
    >
    > "Eliyahu Goldin" wrote:
    >
    > > var txtBox=document,getElementById( "txtUserName");
    > > if (txtBox!=null)
    > > txtBox.focus();
    > >
    > >
    > > Eliyahu
    > >
    > > <> wrote in message
    > > news:...
    > > > I have a home page with username and password textboxes and a login
    > > > button for purposes of users being able to log in (forms
    > > > authentication) directly on the site home page. I also have a dedicated
    > > > "Login.aspx" page, but I don't want users redirected there unless
    > > > absolutely necessary.
    > > >
    > > > I have registered a javascript block via "Page.RegisterStartupScript"
    > > > that sets focus to the "txtUserName" textbox control on Page Load. It
    > > > works great for IE and Firefox.
    > > >
    > > > But...
    > > >
    > > > The problem is that the "txtUserName" textbox does not exist once the
    > > > user has logged in and the page posts back. The code behind then knows
    > > > that the user is authenticated, and so it does a ".Visible=false" for
    > > > the "txtUserName", "txtPassword" and "btnLogin" controls.
    > > >
    > > > Then, when the page loads after the postback, the javascript block
    > > > causes an error icon in the lower left corner of the browser.
    > > > Apparently it is still trying to set focus to the control? I thought I
    > > > had this fixed when I set the code behind to register the page startup
    > > > script only when the page loaded for the first time...but that didn't
    > > > work for some reason, because it still tries to set focus on postbacks.
    > > > I also tried disabling the page viewstate along with the above, but
    > > > that didn't work either.
    > > >
    > > > Can the javascript block be coded to test if the "txtUserName"
    > > > (document.txtUserName ?) textbox exists before trying to set focus to
    > > > it? If so then that would do it...but how would the code look? Also, is
    > > > there another solution that I am not seeing here? I need to get this
    > > > working correctly.
    > > >
    > > > Any suggestions are appreciated. Thanks.
    > > >

    > >
    > >
    > >
    =?Utf-8?B?TWljaGFlbCBCYWx0aWM=?=, Jul 14, 2005
    #6
  7. Guest

    I agree with totally...and I tried that. I put the
    Page.RegisterStartupScript code line within the "if(!Page.IsPostBack)
    block. However, that did not work for some reason. The script continued
    to be emitted within the HTML for postbacks. I am not sure why.

    But, I did get it working properly. Thanks.
    , Jul 15, 2005
    #7
  8. I never liked the idea of attaching scripts from the server code. If the
    script is short, you won't feel any difference if it or there or not. If it
    is long, say more then 20 lines of code, you can put it in a separate file
    that will be cached on client side.

    Eliyahu

    "Michael Baltic" <> wrote in message
    news:...
    > Ideally, you should remove the scripts that are not needed for the current
    > execution of the page.
    >
    > On page_load, if the request is a not a postback, attach the scripts to

    the
    > startup for the textbox focus. If the request is a postback, then don't
    > attach the scripts.
    > --
    > Direct Email: Michael.Baltic@RemoveCharactersUpTo#NCMC.Com
    >
    > Staff Consultant II
    > Enterprise Web Services
    > Cardinal Solutions Group
    >
    >
    > "Sreejith Ram" wrote:
    >
    > > You can also do
    > >
    > > if(typeof(txtUserName) != "undefined")
    > > txtBox.focus();
    > >
    > > Sreejith
    > >
    > > "Eliyahu Goldin" wrote:
    > >
    > > > var txtBox=document,getElementById( "txtUserName");
    > > > if (txtBox!=null)
    > > > txtBox.focus();
    > > >
    > > >
    > > > Eliyahu
    > > >
    > > > <> wrote in message
    > > > news:...
    > > > > I have a home page with username and password textboxes and a login
    > > > > button for purposes of users being able to log in (forms
    > > > > authentication) directly on the site home page. I also have a

    dedicated
    > > > > "Login.aspx" page, but I don't want users redirected there unless
    > > > > absolutely necessary.
    > > > >
    > > > > I have registered a javascript block via

    "Page.RegisterStartupScript"
    > > > > that sets focus to the "txtUserName" textbox control on Page Load.

    It
    > > > > works great for IE and Firefox.
    > > > >
    > > > > But...
    > > > >
    > > > > The problem is that the "txtUserName" textbox does not exist once

    the
    > > > > user has logged in and the page posts back. The code behind then

    knows
    > > > > that the user is authenticated, and so it does a ".Visible=false"

    for
    > > > > the "txtUserName", "txtPassword" and "btnLogin" controls.
    > > > >
    > > > > Then, when the page loads after the postback, the javascript block
    > > > > causes an error icon in the lower left corner of the browser.
    > > > > Apparently it is still trying to set focus to the control? I thought

    I
    > > > > had this fixed when I set the code behind to register the page

    startup
    > > > > script only when the page loaded for the first time...but that

    didn't
    > > > > work for some reason, because it still tries to set focus on

    postbacks.
    > > > > I also tried disabling the page viewstate along with the above, but
    > > > > that didn't work either.
    > > > >
    > > > > Can the javascript block be coded to test if the "txtUserName"
    > > > > (document.txtUserName ?) textbox exists before trying to set focus

    to
    > > > > it? If so then that would do it...but how would the code look? Also,

    is
    > > > > there another solution that I am not seeing here? I need to get this
    > > > > working correctly.
    > > > >
    > > > > Any suggestions are appreciated. Thanks.
    > > > >
    > > >
    > > >
    > > >
    Eliyahu Goldin, Jul 17, 2005
    #8
  9. catherine sea

    Joined:
    Jun 30, 2009
    Messages:
    4
    Thank you, Eliyahu. It helps.
    catherine sea, Oct 26, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xenophon

    Test Link Page Exists?

    xenophon, Feb 14, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    352
    Carlos Albert
    Feb 14, 2006
  2. D. Shane Fowlkes

    checking if textbox actually exists on page...

    D. Shane Fowlkes, Mar 1, 2006, in forum: ASP .Net
    Replies:
    9
    Views:
    3,212
    D. Shane Fowlkes
    Mar 1, 2006
  3. Totan
    Replies:
    0
    Views:
    937
    Totan
    Apr 17, 2006
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,136
  5. Steve
    Replies:
    4
    Views:
    208
    Steve
    Feb 7, 2006
Loading...

Share This Page