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

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

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

    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 Guest

  2. Similar Questions and Discussions

    1. ASP.NET User Controls With Javascript
      *** Sent via Developersdex http://www.developersdex.com ***
    2. How to access values of constituent controls in a User Control
      Hi All, I am building what I thought was a very simple user control in ASP.NET. The control is just a standard asp.net calendar control and a...
    3. user controls and javascript
      Hi there, I have a user control and I need to carry out some client- side custom validation on the controls within the user control (hope that...
    4. howto access controls in User Controls
      "Derda" <safsar@luckyeye.com> wrote in news:e02BoShcDHA.1872 @TK2MSFTNGP12.phx.gbl: From the client bowser? You can't. -- Lucas Tam...
    5. Can not access properties of controls of user control???
      Hi, I have created a user control, and need to change the properties of controls on the user control. However, if the form of user control are...
  3. #2

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

    "Faizan Ahmed" <faizan@edevtech.com> wrote in message
    news:OG%23cg1k8EHA.2900@TK2MSFTNGP09.phx.gbl...
    > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139