using javascript in User controls to access server controls of the user control

Discussion in 'ASP .Net Web Controls' started by Faizan Ahmed, Jan 4, 2005.

  1. Faizan Ahmed

    Faizan Ahmed Guest

    Hello all,
    I have an asp.net textbox (named txtHidden) and an HtmlButton(named
    btnAction). I wanted to write a javascript function which will get called
    when the btnAction is clicked. The function is as follows

    function HideText()
    {
    document.all.item("txtHidden").style.visibility = "hidden";
    }

    This function works fine when i run it in an asp.net page.
    The problem is that it doesn't work inside a user control. i.e. lets say i
    place the two controls and the javascript to work on them, in a user
    control(say MyUC), then the statement document.all.item("txtHidden") does
    not return anything.

    After a bit of effort i've been able to identify that when a user control is
    placed on a page, asp.net, while rendering the page appends all the child
    control of a user control with the client id of the user control to avoid
    naming conflicts. So after the page is rendered the id of "txtHidden" is
    changed to "myUC1_txtHidden".
    Since at the design time of UC we dont know the ClientID of the user
    controls, therefore this makes it impossble to write javascripts in a user
    control, which is exactly what i want to do.

    Any help will be highly appreciated.

    Regards
    Faizan Ahmed
    Faizan Ahmed, Jan 4, 2005
    #1
    1. Advertising

  2. "Faizan Ahmed" <> wrote in message
    news:OG%...
    > Hello all,
    > I have an asp.net textbox (named txtHidden) and an HtmlButton(named
    > btnAction). I wanted to write a javascript function which will get called
    > when the btnAction is clicked. The function is as follows


    ....

    > Since at the design time of UC we dont know the ClientID of the user
    > controls, therefore this makes it impossble to write javascripts in a user
    > control, which is exactly what i want to do.


    Why do you need access to the ClientID at design time? You only need it at
    run-time.

    At run-time (perhaps in the PreRender event), you can create and set the
    JavaScript:

    string script = string.Format("HideText('{0}');", txtHidden.ClientID);
    btnAction.Attributes("onclick") = script;

    I'd change HideText as follows:

    function HideText(string textBoxID)
    {
    document.all.item(textBoxID).style.visibility = "hidden";
    }

    John Saunders
    John Saunders, Jan 4, 2005
    #2
    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. Matthew Louden
    Replies:
    1
    Views:
    6,912
    Scott M.
    Oct 11, 2003
  2. =?Utf-8?B?UGVyZG8=?=
    Replies:
    0
    Views:
    579
    =?Utf-8?B?UGVyZG8=?=
    Feb 6, 2004
  3. george d lake
    Replies:
    1
    Views:
    8,723
    Chris Jackson
    Feb 19, 2004
  4. Chris Zopers

    Access user control from other user control

    Chris Zopers, Feb 20, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    572
    Nanda Lella[MSFT]
    Apr 27, 2009
  5. Faizan Ahmed
    Replies:
    1
    Views:
    282
    John Saunders
    Jan 4, 2005
Loading...

Share This Page