Click here to Skip to main content
13,357,648 members (58,140 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 5 Nov 2013

Access ASP.NET Server Controls in JavaScript Files

, 5 Nov 2013
Rate this:
Please Sign up or sign in to vote.
How to acccess ASP.NET server controls in JavaScript files


This tip describes the way to access the ASP.NET server controls in JavaScript files. This facilitates to move the JavaScript code to a separate JS file.

Using the Code

Follow the steps given below:

  1. Create an aspx file and create an object that will be used to contain all the control ids that you need to access from JavaScript file. Adjust the path and name for the jquery min.js accordingly:
    <script src="../js/jquery.ui-1.8.6.min.js" 
    //Initialize variable to be used in the RecurrentAppointment.js
            var RecurrentRelatedIDs = {};
  2. Initialize the control in $(document.ready) / $(function) so this control can be accessed from js file and call the js file function:
    $(function () {
                //Initialiaze the controls for RecurrentAppointment.js
                RecurrentRelatedIDs = {
                    txtAppointmentDateID: '<%= txtAppointmentDate.ClientID%>'
                  , txtStartsOnDateID: '<%= txtStartsOnDate.ClientID%>'  
  3. Create the js file and add 'function FunctionsToRunAtDocumentReady()' in the js file. This function is called from main page after the control id object is initialized. The js file can access the server controls via the object created earlier:
    function FunctionsToRunAtDocumentReady(){
    RecurrentRelatedIDs.txtAppointmentDateID = 1; 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Muhammad Waqas Iqbal
Software Developer (Senior) Freelancer
Pakistan Pakistan
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 5 Nov 2013
Article Copyright 2013 by Muhammad Waqas Iqbal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid