Referencing a control in Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    Referencing a control in Javascript

    Hi all,

    In my code-behind, I validate some controls and if a particular control has
    a problem, I not only send an alert down to the client via
    Page.RegisterSt artupScript but I'd like to set the focus of the control. now
    the control in question is embedded into a user control which is, in turn,
    embedded into a user control which, in turn, is embedded onto a web form.

    The Javascript does not work if I when I refernce the control with it's ID.
    I checked the client source and saw it was renamed to
    '_ctl2_UCIngDet ail_lstUnits2'. Now when I changed the Javascript to
    'document.frmMa in._ctl2_UCIngD etail_lstUnits2 .focus();', this worked. but
    isn't this going to break if controls are added to the usercontrol?

    What is the correct way to solve this dilemma?

    Regards
    John.


  • PJ Lee

    #2
    Re: Referencing a control in Javascript

    You will need to use the "ClientID" property of the control to reference it
    properly.


    "John" <a@b.com> wrote in message
    news:#U#wyG2QDH A.2592@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hi all,
    >
    > In my code-behind, I validate some controls and if a particular control[/color]
    has[color=blue]
    > a problem, I not only send an alert down to the client via
    > Page.RegisterSt artupScript but I'd like to set the focus of the control.[/color]
    now[color=blue]
    > the control in question is embedded into a user control which is, in turn,
    > embedded into a user control which, in turn, is embedded onto a web form.
    >
    > The Javascript does not work if I when I refernce the control with it's[/color]
    ID.[color=blue]
    > I checked the client source and saw it was renamed to
    > '_ctl2_UCIngDet ail_lstUnits2'. Now when I changed the Javascript to
    > 'document.frmMa in._ctl2_UCIngD etail_lstUnits2 .focus();', this worked. but
    > isn't this going to break if controls are added to the usercontrol?
    >
    > What is the correct way to solve this dilemma?
    >
    > Regards
    > John.
    >
    >[/color]


    Comment

    • David Waz...

      #3
      Re: Referencing a control in Javascript

      js ="<script ...>" & txtBox.ClientID & ".focus();</scirpt>"


      "John" <a@b.com> wrote in message
      news:#U#wyG2QDH A.2592@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi all,
      >
      > In my code-behind, I validate some controls and if a particular control[/color]
      has[color=blue]
      > a problem, I not only send an alert down to the client via
      > Page.RegisterSt artupScript but I'd like to set the focus of the control.[/color]
      now[color=blue]
      > the control in question is embedded into a user control which is, in turn,
      > embedded into a user control which, in turn, is embedded onto a web form.
      >
      > The Javascript does not work if I when I refernce the control with it's[/color]
      ID.[color=blue]
      > I checked the client source and saw it was renamed to
      > '_ctl2_UCIngDet ail_lstUnits2'. Now when I changed the Javascript to
      > 'document.frmMa in._ctl2_UCIngD etail_lstUnits2 .focus();', this worked. but
      > isn't this going to break if controls are added to the usercontrol?
      >
      > What is the correct way to solve this dilemma?
      >
      > Regards
      > John.
      >
      >[/color]


      Comment

      Working...