Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    36
    Thanks
    6
    Thanked 0 Times in 0 Posts

    can't access web control from javascript function

    This is a question more about an ASP.NET web application, but javascript is involved, so I'm hoping some javascript gurus can assist me.

    I'm working with a web application that needs to get access to an ASP.NET web control (a button) in javascript. The problen is that since the control is run on the server, javascript can't access it in the standard way (i.e. document.getElementById(controlId); ).

    I've actually solved this problem before in a different application, but my solution there doesn't seem to work here for some reason.

    I have this in an aspx file:

    Code:
    <script...>
    
    ...
    
    function myfun()
    {
    var b = document.getElementById("<%=SaveButton.ClientID%>");
    alert(b);
    }
    
    </script>
    
    ...
    
    <asp:Button ID="SaveButton" Text="save" OnClientClick="myfun()" ClientIDMode="Static" runat="server" UseSubmitBehavior="False" />
    I have a designer class in which the button is declared (and therefore exists in the server-side codebehind):

    Code:
    protected global::System.Web.UI.WebControls.Button SaveButton;
    But when I click on my button, the alert box says "null".

    Why am I not able to get my button in the javascript function?

    Some things to note:

    *The button exists within a content tag:

    Code:
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    *I'm working within Visual Studios 2008.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Bring up the page in the browser.

    Click on the VIEW menu of the browser.

    Click on the SOURCE or PAGE SOURCE menu item.

    Now look at the HTML that your ASP.NET page is generating.

    Find your button in that HTML.

    If that doesn't help you with the JavaScript, then show the relevant HTML here. Maybe 10 or so lines of HTML on either side of the button in question.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    gib65 (03-24-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ummm...but there is likely a much simpler way.

    I just noticed that you are trying to get the reference to the SAME BUTTON that was clicked on, right?

    So...
    Code:
    <asp:Button ID="SaveButton" Text="save" OnClientClick="myfun(this)"
                ClientIDMode="Static" runat="server" UseSubmitBehavior="False" />
    And then:
    Code:
    function myfun( theClickedOnButton )
    {
        // example code only:
        alert( "id of that button is " + theClickedOnButton.id 
              + "\nvalue of that button is " + theClickedOnButton.value );
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    gib65 (03-24-2012)

  • #4
    New Coder
    Join Date
    Nov 2011
    Posts
    36
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Both solutions work! Thanks to both of you!

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by gib65 View Post
    Both solutions work! Thanks to both of you!
    Sadly, there is now only one of us. My brother was an only child.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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