Hidden objects given error

Discussion in 'ASP .Net' started by tshad, Feb 24, 2007.

  1. tshad

    tshad Guest

    I have some textboxes that are being hidden using <div runat="server"> which
    mean they are not on the page

    But I am also trying to set focus to a Textbox when it is on the screen.
    When the Textbox is not on the screen I am getting a JavaScript error:

    document.forms.0.Email is null or not an object.

    I tried to fix this by doing a test to see if it was not nothing. If it was
    nothing, I would set the <body> tag to "". I tried this in the Page_Load as
    well as Page_PreRender but this doesn't work either - like so:

    Sub Page_PreRender(sender as Object, e as EventArgs)
    if not Email is nothing then
    myBody.Attributes.Add("onLoad","document.forms[0].Email.focus()")
    else
    myBody.Attributes.Add("onLoad","")
    end if
    End Sub

    Is there a way to make this work?

    Thanks,

    Tom
     
    tshad, Feb 24, 2007
    #1
    1. Advertising

  2. tshad wrote:
    > I have some textboxes that are being hidden using <div runat="server"> which
    > mean they are not on the page
    >
    > But I am also trying to set focus to a Textbox when it is on the screen.
    > When the Textbox is not on the screen I am getting a JavaScript error:
    >
    > document.forms.0.Email is null or not an object.
    >
    > I tried to fix this by doing a test to see if it was not nothing. If it was
    > nothing, I would set the <body> tag to "". I tried this in the Page_Load as
    > well as Page_PreRender but this doesn't work either - like so:
    >
    > Sub Page_PreRender(sender as Object, e as EventArgs)
    > if not Email is nothing then
    > myBody.Attributes.Add("onLoad","document.forms[0].Email.focus()")
    > else
    > myBody.Attributes.Add("onLoad","")
    > end if
    > End Sub
    >
    > Is there a way to make this work?
    >
    > Thanks,
    >
    > Tom
    >
    >


    As you are checking if the server control exists, the check will never
    come out false, as the server control always exists even if it's not
    rendered to the final page.

    Check instead the visibility of the control that is used to hide the
    textbox.

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Feb 24, 2007
    #2
    1. Advertising

  3. In addtion to Goran's comments, you don't need to write your own set focus
    code. there ASP.NET 2.0 introduced SetFocus function:

    -- aspx code --
    <div runat="server" id="container">
    <asp:TextBox runat="server" ID="txtEmail"/>
    </div>
    -- end aspx code--

    -- code behind --
    Sub Page_PreRender(sender as Object, e as EventArgs)
    if container.Visible then
    SetFocus(txtEmail)
    ' or another way
    SetFocus(txtEmail.ClientID)
    end if
    End Sub
    -- end code behind --
    Please also note you cannot use control ID in javascript, you have to use
    ClientID in conjunction with document.getElementById() javascript function:

    -- aspx code --
    <script type="text/javascript">
    var texbox = document.getElementById('<%=txtEmail.ClientID%>');
    </script>
    -- end aspx code --




    -- end code behind --
    --
    Milosz


    "tshad" wrote:

    > I have some textboxes that are being hidden using <div runat="server"> which
    > mean they are not on the page
    >
    > But I am also trying to set focus to a Textbox when it is on the screen.
    > When the Textbox is not on the screen I am getting a JavaScript error:
    >
    > document.forms.0.Email is null or not an object.
    >
    > I tried to fix this by doing a test to see if it was not nothing. If it was
    > nothing, I would set the <body> tag to "". I tried this in the Page_Load as
    > well as Page_PreRender but this doesn't work either - like so:
    >
    > Sub Page_PreRender(sender as Object, e as EventArgs)
    > if not Email is nothing then
    > myBody.Attributes.Add("onLoad","document.forms[0].Email.focus()")
    > else
    > myBody.Attributes.Add("onLoad","")
    > end if
    > End Sub
    >
    > Is there a way to make this work?
    >
    > Thanks,
    >
    > Tom
    >
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Feb 24, 2007
    #3
  4. Instead, check to see if a control is visible or enabled. If it's not
    visible it's still there, just has the visibility to false. Usually though I
    run this on the container object that I used to hide it to begin with such
    as a div element, but normally a panel object.


    --

    Hope this helps,
    Mark Fitzpatrick
    Former Microsoft FrontPage MVP 199?-2006

    "tshad" <> wrote in message
    news:%...
    >I have some textboxes that are being hidden using <div runat="server">
    >which mean they are not on the page
    >
    > But I am also trying to set focus to a Textbox when it is on the screen.
    > When the Textbox is not on the screen I am getting a JavaScript error:
    >
    > document.forms.0.Email is null or not an object.
    >
    > I tried to fix this by doing a test to see if it was not nothing. If it
    > was nothing, I would set the <body> tag to "". I tried this in the
    > Page_Load as well as Page_PreRender but this doesn't work either - like
    > so:
    >
    > Sub Page_PreRender(sender as Object, e as EventArgs)
    > if not Email is nothing then
    > myBody.Attributes.Add("onLoad","document.forms[0].Email.focus()")
    > else
    > myBody.Attributes.Add("onLoad","")
    > end if
    > End Sub
    >
    > Is there a way to make this work?
    >
    > Thanks,
    >
    > Tom
    >
    >
     
    Mark Fitzpatrick, Feb 24, 2007
    #4
    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. Lord0
    Replies:
    1
    Views:
    573
    Thomas Weidenfeller
    Apr 19, 2006
  2. chiara
    Replies:
    6
    Views:
    478
    Barry Schwarz
    Oct 6, 2005
  3. 2Barter.net
    Replies:
    0
    Views:
    372
    2Barter.net
    Dec 13, 2006
  4. Casey Hawthorne
    Replies:
    385
    Views:
    5,705
    ng2010
    Apr 4, 2010
  5. Rick
    Replies:
    3
    Views:
    8,201
    Alexey Smirnov
    Apr 13, 2010
Loading...

Share This Page