How to get asp.net client id at external javascript file

前端 未结 5 1538
暖寄归人
暖寄归人 2020-11-28 12:58

When I use embedded javascript functions I can get client id of elements with this code:

document.getElementById(\'<%=buttonXXX         


        
5条回答
  •  盖世英雄少女心
    2020-11-28 13:33

    I can suggest 2 ways.

    First way

    define your variables before call the javascript, inside the .aspx file that can be compiled.

    var ButtonXXXID = <%=buttonXXX.ClientID%>
    // and now include your javascript and use the variable ButtonXXXID
    

    Second way

    in the external javascript file, write your code as:

    function oNameCls(ControlId1) {
    
        this.ControlId1 = ControlId1;
    
        this.DoYourWork1 = function() {
            // use the control id.
            // this.ControlId1
        }   
    }
    

    And call your actions like.

    
    

    calling the action this way you prevent overwrite one action from one control with the same action from other controls on the same page.

提交回复
热议问题