简体   繁体   English

在javascript中抓取asp.net控件

[英]Grabbing asp.net controls in javascript

I have a page where it is getting overwhelmed with code like: 我有一个页面,它被代码所淹没,如:

var textBox = $get("<%=textState.ClientID%>");

This requires me to have my JavaScript inside the page instead of nicely tucked in a js file. 这需要我在页面内部使用我的JavaScript而不是很好地隐藏在js文件中。 Is there a better approach to this? 有更好的方法吗?

document.getElementById doesn't work because then I end up with code like this: document.getElementById不起作用,因为我最终得到这样的代码:

var textBox = document.getElementById("originDestinationControl_textState");

or 要么

var textBox = document.getElementById("ctl00_ContentPlaceHolder_originDestinationControl_textState");

depending on where I am referencing these controls (inside master pages and/or usercontrols) 取决于我引用这些控件的位置(在母版页和/或用户控件内)

I normally stick something like this into pages where I want to use a separate js file. 我通常会将这样的东西粘贴到我想要使用单独的js文件的页面中。

<script type="text/javascript">
    var pageNameElements = {
        textbox : '<%= textbox.ClientId %>',
        button : '<%= button.ClientId %>'
    };
</script>

This way you get a nice javascript object with all the control ids that you can use in your js file like this. 通过这种方式,您可以获得一个很好的javascript对象,其中包含您可以在js文件中使用的所有控件ID。

$('#' + pageNameElements.textbox)

or 要么

document.getElementById(pageNameElements.textbox)

if you're not using jquery. 如果你不使用jquery。

Might I suggest learning jQuery? 我可以建议学习jQuery吗? Since I started using it I have never once had to deal with messy asp tags to get at controls on the page. 自从我开始使用它以来,我从来没有必须处理凌乱的asp标签来获取页面上的控件。

var textBox = $get("<%=textState.ClientID%>");

would look something like 看起来像

var textBox = $("input[id$='_textState']");

in jQuery. 在jQuery中。 And even better, you can place it into it's own js file! 更好的是,你可以将它放入它自己的js文件中!

With .NET 4.0 you actually have total control over this: 使用.NET 4.0,您实际上可以完全控制它:

http://www.codeproject.com/KB/aspnet/ASP_NET4_0ClientIDFeature.aspx http://www.codeproject.com/KB/aspnet/ASP_NET4_0ClientIDFeature.aspx

It has hit Release Candidate along with Visual Studio 2010. I know this isn't an ideal solution, but it is one. 它与Visual Studio 2010一起发布了Release Candidate。我知道这不是一个理想的解决方案,但它是一个。

http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx

I think you're stuck. 我觉得你被卡住了。

Your textState.ClientId code needs to stay on the aspx page itself. 您的textState.ClientId代码需要保留在aspx页面本身。 This is because it's rendered client side. 这是因为它是客户端。 JavaScript include files are downloaded separately from the rest of the page, so it won't know how to handle it. JavaScript包含文件是从页面的其余部分单独下载的,因此它不知道如何处理它。

If you need to clean this up, you could try using classic asp style include files. 如果你需要清理它,你可以尝试使用经典的asp风格包含文件。 However, in asp.net they end up adding more of a mess than they fix. 然而,在asp.net中,他们最终添加了比他们修复的更多的混乱。

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

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