AJAX & ASP.net, links to server controls in an external file

I have JavaScript on an ASP.NET page that looks like this:

var list = $get('<%=Topics.ClientID %>');

I have many functions written now that use this syntax, and I would like to centralize my JavaScript and move it to an external JavaScript file. However, this breaks down, since Themes cannot be found.

What is the best strategy for working? I assume that I should pass control / control information as a parameter to the function, but I cannot get the syntax to work. Any suggestions?

+3
source share
3 answers

ASP.NET JS. , , .

OOP Javascript, JS :

function CouponManager()
{
}

.aspx :

<script language="javascript">
    var couponManager = new CouponManager();
</script>

, :

function CouponManager(params)
{
    // .. stuff here

    this.initialize = function(initParams)
    {
       // .. accessing initParams variable for server control IDs
    };

    this.initialize(params);
}

.aspx :

<script language="javascript">
    var couponManager = new CouponManager
    ({
        txtCouponNameId = '<%= txtCouponName.ClientID %>',
        txtCouponDescriptionId = '<%= txtCouponDescription.ClientID %>'
    });
</script>

JS .aspx .

+1

javascript- usercontol, . /

User Control

 <script language="javascript">
     function GetTopics() {
          return = $get('<%=Topics.ClientID %>');
     } 
 </script>

/

 <script language="javascript">
     var list = GetTopics();
 </script>

. , , - .ClientID, . , - . , . jQuery, , $( ". Topics" ).

+1

, , "" , , (, HiddenField, ) ClientId getter, :

http://andreascode.wordpress.com/2009/04/27/tiny-drips-of-aspnet-juice/

then you can find out in your javascript files that the page will have a hidden field with the identifier set to "Themes" and use it directly.

depending on your domain / situation, this can either save you a lot of time or cause you great harm.

0
source

Source: https://habr.com/ru/post/1709098/


All Articles