如何阻止 ASP.NET 更改 ID 以使用 jQuery [英] How to stop ASP.NET from changing IDs in order to use jQuery

查看:27
本文介绍了如何阻止 ASP.NET 更改 ID 以使用 jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页中有这个标签控件

I have this label control in my web page

<asp:Label ID="Label1" runat="server" Text="test"></asp:Label>

当页面呈现时,控件的 id 更改为这样的内容

And when the page rendered the id of the control changes to something like this

  <span id="ctl00_ContentPlaceHolder1_Label3">test</span>

如何阻止 asp.net 更改 ID 以执行这样的 jQuery 操作

How can I stop asp.net from changing IDs in order to perform a jQuery operation like this

$('#label1').html(xml);

推荐答案

而不是使用这个选择器

$('#Label1')

使用这个,基本上你使用的是经典的asp内联服务器端代码.

use this one, basically you are using the classic asp inline server side code.

$('#<%= Label1.ClientID %>')

这将插入生成的任何 ID 以作为文字放置.

this will insert whatever ID that is generated to be placed as a literal.

如果您希望使用外部文件,那么我建议您在页面上创建一个全局的 obj 来保存您的所有客户端 ID,然后在您的外部文件中引用该 obj(不理想,但它是一个解决方案)

If you wish to use external files then I would suggest you create an obj that is global on the page to hold all of your client ids and then reference that obj within your external files (not ideal but it is a solution)

var MyClientIDs = {
    Label1 = '<%= Label1.ClientID %>',
    Label2 = '<%= Label2.ClientID %>',
    Textbox1 = '<%= Textbox1.ClientID %>',
    Textbox2 = '<%= Textbox2.ClientID %>'
};

然后在您的外部文件中,您可以访问 Label1,例如:$('#' + MyClientIDs.Label1)

and then in your external file you can access Label1 for example: $('#' + MyClientIDs.Label1)

这篇关于如何阻止 ASP.NET 更改 ID 以使用 jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆