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

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

问题描述

我有这样的标签控制在我的网页

 < ASP:标签ID =标签1=服务器文本=测试>< / ASP:标签>
 

和当页面呈现的控件更改ID为这样的事情

 <跨度ID =ctl00_ContentPlaceHolder1_Label3>测试< / SPAN>
 

如何才能执行操作的jQuery这样我可以停止asp.net更改标识

  $('#LABEL1)HTML(XML)。
 

解决方案

而不是使用此选择

  $('#标签1)
 

使用这一块,基本上你使用的是传统的ASP内联服务器端code。

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

这将插入任何的ID生成放置的文字。

如果您希望使用外部文件的话,我建议你创建一个OBJ是在页面上全球容纳所有的客户端ID,然后引用OBJ外部文件中(不理想,但它是一个解决方案)

  VAR MyClientIDs = {
    标签1 ='<%= Label1.ClientID%>',
    标签2 ='&其中;%= Label2.ClientID%GT;',
    Textbox1的='&其中;%= Textbox1.ClientID%GT;',
    TextBox2中='&其中;%= Textbox1.ClientID%GT;'
};
 

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

I have this label control in my web page

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

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

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

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

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

解决方案

instead of using this selector

$('#Label1')

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

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

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

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 = '<%= Textbox1.ClientID %>'
};

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

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

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