JavaScript with User Control

Discussion in 'ASP .Net' started by Steve Murphy, Aug 24, 2005.

  1. Steve Murphy

    Steve Murphy Guest

    I'd like to package a navigation bar into a user control. The problem I'm
    having is getting the rollover buttons to work. Where do I place the
    Javascript for the image caching and switching for the rollover effect? I'm
    using image buttons.

    Thanks in advance,
    Steve Murphy
    Steve Murphy, Aug 24, 2005
    #1
    1. Advertising

  2. I would place the JavaSCript in the Control itself.

    I've often had problems with JavaScript in UserControls, as they are renamed
    when placed onto the ASPX page.
    When you register your ASPX page, and give your tag a name, in the output
    HTML that goes to INternet explorer, you controls are renamed the tag name
    followed by the ID you specified.

    So if you have a textbox:
    <asp:TextBox id="tb"
    and you register your usercontrol
    TagName="MyUserControl"
    In I.E. the Textbox will now have the ID
    MyUserControl_tb

    So when you have javascript thats referring to this ID, it will stop working
    untill you change this to refer to MyUserControl_tb.
    Now this becomes a bit silly, because that ID name is based on its instance
    in a given ASPX page, and cannot be generalised.

    There was a post a few days ago about how to get the controls name from the
    code and then generate the JavaScript there with thee right ID;s
    I'm just gonna dig that up (cause i need it to for future reference), and
    will post shortly

    HTH



    "Steve Murphy" <> wrote in message
    news:...
    > I'd like to package a navigation bar into a user control. The problem I'm
    > having is getting the rollover buttons to work. Where do I place the
    > Javascript for the image caching and switching for the rollover effect?
    > I'm using image buttons.
    >
    > Thanks in advance,
    > Steve Murphy
    >
    >
    Grant Merwitz, Aug 24, 2005
    #2
    1. Advertising

  3. ok, found it

    This i have not tested, but will next time i have this problem

    btnMyButton.UniqueID

    I think .. THINK .. if you creating your JavaScript from your Code Behind,
    when you try do something like
    document.GetElementByID('btnMyButton');
    swap this for

    document.GetElementByID('" + btnMyButton.UniqueID + "')
    and it should use the right ID

    The same logic would go for ImageButtons with rollovers - if you need to
    reference the ImageButtons ID

    HTH

    "Grant Merwitz" <> wrote in message
    news:...
    >I would place the JavaSCript in the Control itself.
    >
    > I've often had problems with JavaScript in UserControls, as they are
    > renamed when placed onto the ASPX page.
    > When you register your ASPX page, and give your tag a name, in the output
    > HTML that goes to INternet explorer, you controls are renamed the tag name
    > followed by the ID you specified.
    >
    > So if you have a textbox:
    > <asp:TextBox id="tb"
    > and you register your usercontrol
    > TagName="MyUserControl"
    > In I.E. the Textbox will now have the ID
    > MyUserControl_tb
    >
    > So when you have javascript thats referring to this ID, it will stop
    > working untill you change this to refer to MyUserControl_tb.
    > Now this becomes a bit silly, because that ID name is based on its
    > instance in a given ASPX page, and cannot be generalised.
    >
    > There was a post a few days ago about how to get the controls name from
    > the code and then generate the JavaScript there with thee right ID;s
    > I'm just gonna dig that up (cause i need it to for future reference), and
    > will post shortly
    >
    > HTH
    >
    >
    >
    > "Steve Murphy" <> wrote in message
    > news:...
    >> I'd like to package a navigation bar into a user control. The problem I'm
    >> having is getting the rollover buttons to work. Where do I place the
    >> Javascript for the image caching and switching for the rollover effect?
    >> I'm using image buttons.
    >>
    >> Thanks in advance,
    >> Steve Murphy
    >>
    >>

    >
    >
    Grant Merwitz, Aug 24, 2005
    #3
  4. Steve Murphy

    Steve Murphy Guest

    "Grant Merwitz" <> wrote:

    > I think .. THINK .. if you creating your JavaScript from your Code Behind,
    > when you try do something like
    > document.GetElementByID('btnMyButton');
    > swap this for
    >
    > document.GetElementByID('" + btnMyButton.UniqueID + "')
    > and it should use the right ID


    Why can't you just use:
    document.GetElementByID('btnMyButton.UniqueID')

    Why do you need to wrap it?

    Thanks,
    Steve Murphy
    Steve Murphy, Aug 24, 2005
    #4
  5. I was referring to the code behind where you would build up the jscript then
    register it on the page

    string MyJScript = "<Script language="javascript">";
    MyJScript += "function something() { document.GetElementByID('" +
    btnMyButton.UniqueID + "');}";
    MyJScript += "</script>";

    Page.RegisterClientScriptBlock("SomeKey", MyJScript");


    as btnMyButton.UniqueID is a c# function, not a JavaScript function



    "Steve Murphy" <> wrote in message
    news:...
    >
    > "Grant Merwitz" <> wrote:
    >
    >> I think .. THINK .. if you creating your JavaScript from your Code
    >> Behind,
    >> when you try do something like
    >> document.GetElementByID('btnMyButton');
    >> swap this for
    >>
    >> document.GetElementByID('" + btnMyButton.UniqueID + "')
    >> and it should use the right ID

    >
    > Why can't you just use:
    > document.GetElementByID('btnMyButton.UniqueID')
    >
    > Why do you need to wrap it?
    >
    > Thanks,
    > Steve Murphy
    >
    >
    >
    Grant Merwitz, Aug 24, 2005
    #5
    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. Tom Rowton
    Replies:
    2
    Views:
    643
    Tom Rowton
    Aug 1, 2003
  2. Mad Scientist Jr
    Replies:
    0
    Views:
    625
    Mad Scientist Jr
    Mar 22, 2006
  3. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    530
    Jonathan Wood
    Feb 2, 2010
  4. Faizan Ahmed
    Replies:
    1
    Views:
    270
    John Saunders
    Jan 4, 2005
  5. Faizan Ahmed
    Replies:
    1
    Views:
    345
    John Saunders
    Jan 4, 2005
Loading...

Share This Page