简体   繁体   English

使用javascript访问ASP.NET控件

[英]Access ASP.NET control using javascript

I have a basic javascript function like this which works as I am able to dynamically change the textbox in a javascript method at runtime. 我有一个像这样的基本javascript函数,它可以在运行时动态更改javascript方法中的文本框,因此可以正常工作。

function MyTst(pVal2) {           
        var x = document.getElementById('<%=myImage.ClientID%>');
        x.src = "/images/write.gif";      

    }

In the above code I need to be generic. 在上面的代码中,我需要通用。 So replace '<%=myImage.ClientID%>' with the input param to my method which in this instance is called pVal2 and contains the asp.net server control image id. 因此,将'<%= myImage.ClientID%>'替换为我方法的输入参数,在此实例中,该方法称为pVal2,其中包含asp.net服务器控件映像ID。

Can anyone advise on an approach to do this. 任何人都可以建议执行此操作的方法。 I've tried lots of things and just cant seem to get it working. 我已经尝试了很多东西,但似乎无法正常工作。

Thanks for any advice. 感谢您的任何建议。

You can change the call of the function to make it more flexible 您可以更改函数的调用以使其更加灵活

MyTst('<%=myTxtBox.ClientID%>') 

function MyTst(pVal2) {           
        var x = document.getElementById(pVal2);
        x.src = "/images/write.gif";      

    }

What about passing element as parameter 传递元素作为参数呢

function MyTst(txtElement) {  

   txtElement.src = "/images/write.gif"; 

}

In mark up use this like onclick="MyTst(this)" to pass current element 在标记了使用thisonclick="MyTst(this)"来传递电流元件

Note : src property belongs to <img> tag and not for <input type=text> 注意src属性属于<img>标记,而不属于<input type=text>

I managed to resolve this with the help of JQuery and this website. 我设法在JQuery和此网站的帮助下解决了这个问题。 So it is entirely possible to access server controls through javascript. 因此,完全有可能通过javascript访问服务器控件。 Here is the helpful page. 这是有用的页面。 enter link description here 在此处输入链接说明

Here is the code I used. 这是我使用的代码。 I hope it of use to somebody. 我希望它对某人有用。

function MyTst(pVal) {
        var x = document.getElementById($("[id$=" + pVal2 + "]").attr("id"));
        x.src = "/images/write.gif";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM