Page 2 of 2 FirstFirst 12
Results 41 to 56 of 56

Thread: UserControl javascript: get control by id

  1. #41
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: UserControl javascript: get control by id

    Nick,

    As you will see, I put an extra alert in the code, on the very first line.

    This meant that I could see that at the very least, the function was getting called. From there, given that there is another alert directly after the offending line, it narrowed down what was happening. Then, on looking at the line, I thought about what could be causing the problem.

    Then I tried this:

    Code:
    alert($(obj));
    I tried this (my jQuery isn't that hot, and I wondered if motil was trying to work magic) and when that didn't return anything, I knew that this was the culprit.

    From there, it was a short step to do this:

    Code:
    alert($(sender));
    And when that returned an HtmlElement, I altered the code as above.

    I hope that little explanation helps.

    Gary

  2. #42

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: UserControl javascript: get control by id

    Sorry, I dunno why I wrote that, it's correct in my code:
    asp Code:
    1. <%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ButtonUserControl.ascx.vb" Inherits="F1TimeTrials.ButtonUserControl" %>
    2.  
    3. <script type="text/javascript">
    4.     function buttonClicked(sender) {
    5.         var label = $(sender).parent().parent().find('span:eq(0)');
    6.         alert('jup');
    7.         label[0].innerHTML += '|';
    8.         return false;
    9.     }
    10. </script>
    11.  
    12. <asp:Table runat="server">
    13.     <asp:TableRow runat="server">
    14.         <asp:TableCell runat="server"><asp:Button runat="server" ID="button" Text="Click!" OnClientClick="return buttonClicked(this);" /></asp:TableCell>
    15.         <asp:TableCell runat="server"><asp:Label runat="server" ID="label" Text="" /></asp:TableCell>
    16.     </asp:TableRow>
    17. </asp:Table>
    Still nothing. No alerts, no post-backs, no | characters.

    I tried debugging with Firebug but unless I'm doing it wrong even that isn't working. I just go to the sripts tab, find the piece of javascript, put a breakpoint beside it, but it doesn't hit when I click the buttons.

    It does hit suddenly when I refresh the page. Then I also get the option like Run, Step Into, Step Over, etc, but whatever I do (step into, over, out) it just seems to skip the entire code in the function. The breakpoint is on the function declaration ("function buttonClicked(sender)").

    EDIT
    And when I put an alert on the very first line of the function it does show, so the function is getting called OK.

    EDIT2
    When I try this javascript, only 'hit' is shown:
    javascript Code:
    1. <script type="text/javascript">
    2.     function buttonClicked(sender) {
    3.         alert('hit');
    4.         alert($(sender));
    5.  
    6.         var label = $(sender).parent().parent().find('span:eq(0)');
    7.         alert('jup');
    8.         label[0].innerHTML += '|';
    9.         return false;
    10.     }
    11. </script>

    EDIT3
    When I alert just 'sender' it shows me something like object HTMLElement so it's not null.

  3. #43
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: UserControl javascript: get control by id

    Ok, put an alert before the "var label" line, does that get hit?

    I literall took your code from post #27, pasted into a new user control, added the user control to a page, and away it went.

    Gary

  4. #44
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: UserControl javascript: get control by id

    yep you doing something wrong, i copied the exact code you posted and its working without changing single character ...

    in firebug script tab, did you made sure thet it's enabled ?
    when you selecting the script tab there is little down arrow, press it and make sure it's enabled ..

    again, i copied the exact code you posted and it's working on my system.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #45
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: UserControl javascript: get control by id

    Nick, again, in order to avoid something simple that is being missed in translation, can you upload a sample application that isn't working and we can take a look?

    Gary

  6. #46

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: UserControl javascript: get control by id

    I guess the jQuery thing isn't working, because it seems that 'sender' is not null while $(sender) is... Or at least, it doens't show an alert if I use $(sender).

    Project is attached.
    Attached Files Attached Files

  7. #47
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: UserControl javascript: get control by id

    Ok, this is an issue with this line:

    Code:
    <script src="~/Scripts/jquery-1.4.1.js" type="text/javascript" />
    Use this:

    Code:
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    Instead, and it will work.

    Gary

  8. #48
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: UserControl javascript: get control by id

    yep jQuery is was not enabled.

    because of this "~":
    Code:
      <script src="~/Scripts/jquery-1.4.1.js" type="text/javascript" />
    change it to this:
    Code:
          <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    you can use the "~" sign only when using ASP.NET controls that has runat="server" attribute

    otherwise just use the slash "/" has the web directory
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  9. #49
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: UserControl javascript: get control by id

    you win this time gap
    it took me time to explain in details
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  10. #50
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: UserControl javascript: get control by id

    Ha ha.

    Yeah, I probably should have provided details.

    Nick, if you drag the .js file from the Solution Explorer into the ASPX/ASCX window, the correct path will be created for you.

    Gary

  11. #51
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: UserControl javascript: get control by id

    again, do not drag js files or define function / js inside your user control, because once you load multiple instances of that usercontrol you also double your js functions / js code, instead create global js file and add reference to the master page (or to the default.aspx file if you're not using master page)
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  12. #52
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: UserControl javascript: get control by id

    Ah, very good point.

    If you intend on having multiple user controls on the page, then defining the JS include at the highest level makes sense.

    The only time I would go ahead this is if you have a very large JS file, that is only used in the user control, and you only intend on using the user control one at a time, that I would say put it in there.

    Gary

    Gary

  13. #53

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: UserControl javascript: get control by id

    Thanks, it finally seems to work ok. I'll go try it out on the slider controls.

    Hm, the sliders stopped working completely. It's probably an unrelated issue but I've got no time to figure it out now so I'll get back to this probably tomorrow.

  14. #54
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: UserControl javascript: get control by id

    ok just remember that the jquery I posted probably won't work right a way with the slider control you got there, adjustments should be made.

    just post here the html markup of a single control and i'll post the correct jq syntax.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  15. #55

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: UserControl javascript: get control by id

    Quote Originally Posted by motil View Post
    ok just remember that the jquery I posted probably won't work right a way with the slider control you got there, adjustments should be made.

    just post here the html markup of a single control and i'll post the correct jq syntax.
    Yeah I figured as much, as the slider is visually quite 'enhanced', contains lots of images and stuff like tickmarks that are probably 'labels' too. But even a separate WebSlider (completely without any of this javascript or even any labels beside it) has stopped working, so I probably broke something else somewhere...

  16. #56
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: UserControl javascript: get control by id

    ok, we here if you need any farther help.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

Page 2 of 2 FirstFirst 12

Posting Permissions

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



Click Here to Expand Forum to Full Width