问题描述
我试图找到应对的ASP.NET prepends一个值,与 =服务器$ C $创建的任何元素的方法的最佳途径C>,而不必诉诸使用
<%= id.ClientID%GT;
。我今天早些时候与以下解决方案来了,但我敢肯定,有更优雅的解决方案:
I'm trying to find the best way to deal with the way that ASP.NET prepends a value to any element that is created with runat="server"
, without having to resort to using <%= id.ClientID %>
. I came up with the following solution earlier today, but I'm sure that there are more elegant solutions:
$(document).ready(function() {
var dotNetPrefix = $("[id$='prepended_ID_value']").attr('id');
if (dotNetPrefix !== undefined) {
dotNetPrefix = dotNetPrefix.replace('prepended_ID_value', '');
dotNetPrefixID = '#' + dotNetPrefix;
console.log('dotNetPrefix = ' + dotNetPrefix);
testDotNet('prepended_ID_value');
} else {
console.log('Hidden Field is Missing! => <asp:HiddenField runat="server" ID="prepended_ID_value" Value="dotNet_ID_Prefix" />');
}
});
function testDotNet(getID) {
var test_dotNetPrefixID = $(dotNetPrefixID + getID).val();
console.log('$(\'' + dotNetPrefixID + getID + '\').val() = ' + test_dotNetPrefixID);
}
这种方法的一个问题是,它需要我放置以下隐藏字段在每一个网页:
&LT; ASP:HiddenField =服务器ID =prepended_ID_valueVALUE =dotNet_ID_ preFIX/&GT;
......这使得它有点klunky(或添加到现有klunkiness因子)。
One problem with this method is that it requires that I place the following hidden field in every page: <asp:HiddenField runat="server" ID="prepended_ID_value" Value="dotNet_ID_Prefix" />
...and that makes it kinda klunky (or adds to the existing klunkiness factor).
我的最终目标是让这个我不再需要使用&LT;%= id.ClientID%GT;
(或任何需要服务器端code),所以我所有的JS都可以称为一个包含,而无需将其嵌入在我的ASPX页面。哦,我的第二个最终目标是,它是尽可能简单,不从项目到项目需要大量的安装时间。
My ultimate goal is to make it so that I no longer have to use <%= id.ClientID %>
(or anything that requires server side code) so that all of my JS can be called as an include instead of having to embed it in my ASPX pages. Oh, my second ultimate goal is that it's as simple as possible and doesn't require a lot of setup time from project to project.
我创建了一个的jsfiddle用一个例子
I created a JSFiddle with an example http://jsfiddle.net/Realto619/8ZZYt/2/
推荐答案
为什么不直接使用的?在Web.config设置它,这样你的客户端ID将整个站点编写的。
Why not just use ClientIDMode=Static? Set it in the Web.config so that your client IDs will be as-written throughout the site.
这篇关于什么是处理ASP.NET的客户端ID的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!