WebForms和jQuery的,如何匹配的ID? [英] Webforms and jQuery, how to match the ID's?

查看:101
本文介绍了WebForms和jQuery的,如何匹配的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery用asp.net webfoms。我需要得到一个特殊的工具包,使.NET控件吐出友好的控制ID的?

I want to use jQuery with asp.net webfoms. Do I need to get a special toolkit so the .net controls spit out friendly Control ID's?

原因是,我不想写javascript引用我的html的ID喜欢control_123_asdfcontrol_234。

Reason being, I don't want to write javascript referencing my html ID's like control_123_asdfcontrol_234.

这种情形已经在3.5版本中解决? (我记得读你已经得到一些特殊的DLL,使得ID的友好)。

Has this been addressed in version 3.5? (I remember reading you have to get some special dll that makes ID's friendly).

推荐答案

我发现只是以匹配大多数控制错位ID的最后的最简单方法。专有技术是单选按钮列表和复选框列出了例外 - 你必须要有点棘手与他们

The easiest way I've found is just to match on the end of the mangled ID for most controls. The exceptions that Know of are radiobutton lists and checkbox lists - you have to be a little trickier with them.

但是,如果你在你的.aspx页面中有这样的:

But if you have this in your .aspx page:

<asp:TextBox ID="txtExample" runat="server" />

那么你的jQuery不难发现,控制,即使它是由母版页呈现错位,是这样的:

Then your jQuery can easily find that control, even if it's mangled by the master page rendering, like this:

$("[id$=txtExample]")

$ = 运营商匹配字符串的结束和名字改编总是在前面。一旦你做到了这一点,就可以得到实际的错位ID是这样的:

The $= operator matches the end of the string and the name mangling is always on the front. Once you've done that, you can get the actual mangled ID like this:

$("[id$=txtExample]").attr("id")

然后解析反正你认为合适的。

and then parse that anyway you see fit.

编辑:
这是一种简单的方法,但也可以是多击不仅仅是给每个一个性能控制一类一样其旧的ID。

This is an easy way, but it may be more of a performance hit than just giving each control a class the same as its old ID.

请参阅本文杰夫张贴在另一个jQuery的优化问题的链接:

See this article that Jeff posted a link to on another jQuery optimization question:

的jQuery:选择器的性能分析

jQuery: Performance Analysis of Selectors

这篇关于WebForms和jQuery的,如何匹配的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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