Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

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

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

document.getElementById('<%=buttonXXX.ClientID%>' )

But now I am using external javascript file for caching and faster rendering and this code does not work any more for getting client id's of elements, it gives error.

How can I get client id's of elements at external javascript file using asp.net 2.0 , netframework 3.5 , c# , iis 7.5

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

<script>
    // init - create
    var <%=this.ClientID%>MyCls = new oNameCls('<%=Control1.ClientID%>');
    // do your work
    <%=this.ClientID%>MyCls.DoYourWork1();
</script>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...