UserControl run Javascript

Discussion in 'ASP .Net' started by TCORDON, Oct 18, 2005.

  1. TCORDON

    TCORDON Guest

    How can I make a UserControl run some javascript when it loads? this is so
    the control sets focus to a textbox it contains.

    TIA!
    TCORDON, Oct 18, 2005
    #1
    1. Advertising

  2. Hi,

    This will do it, just add it to the load event of your User Control:

    If (Page.IsClientScriptBlockRegistered("Focus") = False) Then
    MyBase.Page.RegisterStartupScript("Focus", "<script
    language='javascript'>document.getElementById('" + TextBox1.ClientID +
    "').focus();</script>")
    End If

    Be sure to use control.ClientID and not just hard code the control id
    because ASP.Net will change the control id inside a User Control to somthing
    like "_ctl0_controlid". Good luck! Ken.

    --
    Ken Dopierala Jr.
    For great ASP.Net web hosting try:
    http://www.webhost4life.com/default.asp?refid=Spinlight
    If you sign up under me and need help, email me.

    "TCORDON" <> wrote in message
    news:OIvr$r$...
    > How can I make a UserControl run some javascript when it loads? this is so
    > the control sets focus to a textbox it contains.
    >
    > TIA!
    >
    >
    Ken Dopierala Jr., Oct 18, 2005
    #2
    1. Advertising

  3. TCORDON

    TCORDON Guest

    I added de code you gave me, but I dont know if I am doing something wrong,
    the focus is not being set. This is how my conde looks now:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    'Put user code to initialize the page here

    If (Page.IsClientScriptBlockRegistered("Focus") = False) Then

    MyBase.Page.RegisterStartupScript("Focus",
    "<scriptlanguage='javascript'>document.getElementById('" + txtLinea.ClientID
    + "').focus();</script>")

    End If

    End Sub


    "Ken Dopierala Jr." <> wrote in message
    news:uOu6Vz$...
    > Hi,
    >
    > This will do it, just add it to the load event of your User Control:
    >
    > If (Page.IsClientScriptBlockRegistered("Focus") = False) Then
    > MyBase.Page.RegisterStartupScript("Focus", "<script
    > language='javascript'>document.getElementById('" + TextBox1.ClientID +
    > "').focus();</script>")
    > End If
    >
    > Be sure to use control.ClientID and not just hard code the control id
    > because ASP.Net will change the control id inside a User Control to
    > somthing
    > like "_ctl0_controlid". Good luck! Ken.
    >
    > --
    > Ken Dopierala Jr.
    > For great ASP.Net web hosting try:
    > http://www.webhost4life.com/default.asp?refid=Spinlight
    > If you sign up under me and need help, email me.
    >
    > "TCORDON" <> wrote in message
    > news:OIvr$r$...
    >> How can I make a UserControl run some javascript when it loads? this is
    >> so
    >> the control sets focus to a textbox it contains.
    >>
    >> TIA!
    >>
    >>

    >
    >
    TCORDON, Oct 18, 2005
    #3
  4. Hi,

    There needs to be a space here between "script" and "language" :

    > "<scriptlanguage='javascript'>document.getElementById('" +

    txtLinea.ClientID

    If it still doesn't work then do a view source on your page and post the
    outputted HTML. It'll be just above the clsoing </form> tag. Ken.

    --
    Ken Dopierala Jr.
    For great ASP.Net web hosting try:
    http://www.webhost4life.com/default.asp?refid=Spinlight
    If you sign up under me and need help, email me.

    "TCORDON" <> wrote in message
    news:exQGg6$...
    > I added de code you gave me, but I dont know if I am doing something

    wrong,
    > the focus is not being set. This is how my conde looks now:
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    >
    > 'Put user code to initialize the page here
    >
    > If (Page.IsClientScriptBlockRegistered("Focus") = False) Then
    >
    > MyBase.Page.RegisterStartupScript("Focus",
    > "<scriptlanguage='javascript'>document.getElementById('" +

    txtLinea.ClientID
    > + "').focus();</script>")
    >
    > End If
    >
    > End Sub
    >
    >
    > "Ken Dopierala Jr." <> wrote in message
    > news:uOu6Vz$...
    > > Hi,
    > >
    > > This will do it, just add it to the load event of your User Control:
    > >
    > > If (Page.IsClientScriptBlockRegistered("Focus") = False) Then
    > > MyBase.Page.RegisterStartupScript("Focus", "<script
    > > language='javascript'>document.getElementById('" + TextBox1.ClientID +
    > > "').focus();</script>")
    > > End If
    > >
    > > Be sure to use control.ClientID and not just hard code the control id
    > > because ASP.Net will change the control id inside a User Control to
    > > somthing
    > > like "_ctl0_controlid". Good luck! Ken.
    > >
    > > --
    > > Ken Dopierala Jr.
    > > For great ASP.Net web hosting try:
    > > http://www.webhost4life.com/default.asp?refid=Spinlight
    > > If you sign up under me and need help, email me.
    > >
    > > "TCORDON" <> wrote in message
    > > news:OIvr$r$...
    > >> How can I make a UserControl run some javascript when it loads? this is
    > >> so
    > >> the control sets focus to a textbox it contains.
    > >>
    > >> TIA!
    > >>
    > >>

    > >
    > >

    >
    >
    Ken Dopierala Jr., Oct 18, 2005
    #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. huobazi
    Replies:
    1
    Views:
    683
    David Waz...
    Jul 3, 2003
  2. Rajesh Tiwari

    Can we use a usercontrol inside a usercontrol

    Rajesh Tiwari, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    410
    Rajesh Tiwari
    Aug 12, 2003
  3. nail
    Replies:
    0
    Views:
    1,539
  4. Phl
    Replies:
    2
    Views:
    522
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 18, 2004
  5. Fabiano
    Replies:
    2
    Views:
    597
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
Loading...

Share This Page