www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] can't access my web control from javascript

  1. #1
    Join Date
    Apr 2005
    Posts
    35

    resolved [RESOLVED] can't access my web control from javascript

    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. #2
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Have you looked at the resulting source? Does the following line contain the appropriate ID in it?

    PHP Code:
    var document.getElementById("<%=SaveButton.ClientID%>"); 

  3. #3
    Join Date
    Mar 2012
    Posts
    11
    You could also try putting a span or div around the control such as this:

    HTML Code:
    <div id="control_div">
        <control.....>
    </div>
    Then you could try to access it with this:

    document.getElementById("control_div").firstChild

  4. #4
    Join Date
    Apr 2005
    Posts
    35
    Both solutions work, but steve-o's solution only works in IE.

    Thanks both.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles