Accessing invisible control in Javascript

Discussion in 'ASP .Net' started by ipramod@gmail.com, Sep 25, 2007.

  1. Guest

    Hi All,

    I have below label which is invisible when the page loads.
    <asp:Label ID="lblTest" runat="server" Visible="false" Text="Test"></
    asp:Label>


    Now if I want to make this label visible on the web page, I used
    below
    javascript:
    document.getElementById("<%= lblTest.ClientID %>").style.display =
    "block";


    But when the Javascript code is executed, I get below exception:
    "Object required"


    As per my observation, if the control is not loaded at the first time
    then it will not have the client id and it will throw an exception.


    Same is the case with DIV. I have 2 DIV's which are invisible on the
    page load and in special cases I want to make one DIV visible and
    make
    other invisible then I get the above exception.


    So, is there any way to make the controls visible/invisible
    regardless
    of their initial stage?


    Please let me know ASAP.


    Thanks in advance,
    PI
     
    , Sep 25, 2007
    #1
    1. Advertising

  2. Leave Visible=true and hide controls with a css style display:none.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    <> wrote in message
    news:...
    >
    > Hi All,
    >
    > I have below label which is invisible when the page loads.
    > <asp:Label ID="lblTest" runat="server" Visible="false" Text="Test"></
    > asp:Label>
    >
    >
    > Now if I want to make this label visible on the web page, I used
    > below
    > javascript:
    > document.getElementById("<%= lblTest.ClientID %>").style.display =
    > "block";
    >
    >
    > But when the Javascript code is executed, I get below exception:
    > "Object required"
    >
    >
    > As per my observation, if the control is not loaded at the first time
    > then it will not have the client id and it will throw an exception.
    >
    >
    > Same is the case with DIV. I have 2 DIV's which are invisible on the
    > page load and in special cases I want to make one DIV visible and
    > make
    > other invisible then I get the above exception.
    >
    >
    > So, is there any way to make the controls visible/invisible
    > regardless
    > of their initial stage?
    >
    >
    > Please let me know ASAP.
    >
    >
    > Thanks in advance,
    > PI
    >
     
    Eliyahu Goldin, Sep 25, 2007
    #2
    1. Advertising

  3. Guest

    Hey thanks...
    it worked :)


    On Sep 25, 7:50 pm, "Eliyahu Goldin"
    <> wrote:
    > Leave Visible=true and hide controls with a css style display:none.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    >
    > <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > Hi All,

    >
    > > I have below label which is invisible when the page loads.
    > > <asp:Label ID="lblTest" runat="server" Visible="false" Text="Test"></
    > > asp:Label>

    >
    > > Now if I want to make this label visible on the web page, I used
    > > below
    > > javascript:
    > > document.getElementById("<%= lblTest.ClientID %>").style.display =
    > > "block";

    >
    > > But when the Javascript code is executed, I get below exception:
    > > "Object required"

    >
    > > As per my observation, if the control is not loaded at the first time
    > > then it will not have the client id and it will throw an exception.

    >
    > > Same is the case with DIV. I have 2 DIV's which are invisible on the
    > > page load and in special cases I want to make one DIV visible and
    > > make
    > > other invisible then I get the above exception.

    >
    > > So, is there any way to make the controls visible/invisible
    > > regardless
    > > of their initial stage?

    >
    > > Please let me know ASAP.

    >
    > > Thanks in advance,
    > > PI- Hide quoted text -

    >
    > - Show quoted text -
     
    , Sep 25, 2007
    #3
  4. <> wrote in message
    news:...

    > Hey thanks...
    > it worked :)


    Just to add to Eliyahu's response, the reason that it worked (or, rather,
    the reason that it didn't work with Visible="false") is because when you set
    a control's Visible property to false server-side, it doesn't even get
    downloaded to the client...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Sep 25, 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. Dag Sunde
    Replies:
    8
    Views:
    3,097
    Andrew T.
    May 10, 2006
  2. Charlie@CBFC
    Replies:
    6
    Views:
    93,865
    Laurent Bugnion
    May 15, 2006
  3. Andy B
    Replies:
    5
    Views:
    600
    Andy B
    May 29, 2008
  4. TS
    Replies:
    5
    Views:
    865
  5. Replies:
    1
    Views:
    91
    Henry
    Sep 25, 2007
Loading...

Share This Page