访问Asp.net控制使用jQuery(所有选项) [英] Accessing Asp.net controls using jquery (all options)

查看:78
本文介绍了访问Asp.net控制使用jQuery(所有选项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery访问asp.net控制

How to access asp.net control using jquery

< ASP:文本框=服务器ID =myTextBox/>

$('#myTextBox')是行不通的。

推荐答案

< ASP:文本框=服务器ID =myTextBox/>

当一个页面变化呈现上述的aspx code

The above aspx code when rendered on a page changes to

<输入类型=文本ID =ctl00_Main_myTextBoxNAME =ctl00 $主$ myTextBox/>

这是因为主和控制信息,其中.NET控件驻留获得prepended这使得它有点棘手我们写一个选择。

This is because the master and control information in which the .net control resides gets prepended which makes it a little tricky for us to write a selector.

您有几种选择。这绝不是COM prehensive,但我会尝试一下。

You have a few options. This is by no means comprehensive, but I will give it a try.

选项1:

Option1:

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

使用客户端ID - 推荐,但咩..没有这么多。我会尝试,如果我能避免编写客户端ID 。主要的原因是,你只能用它在的.aspx 的网页,而不是外部的.js 文件。

Use the ClientID - recommended but meh.. not so much. I would try to avoid writing ClientID if I could. The primary reason being, you can only use it in .aspx pages and not external .js files.

选项2:

Option2:

$('[id$=myTextBox]') // id which ends with the text 'myTextBox'

$('[id*=myTextBox]') // id which contains the text 'myTextBox'

使用属性选择 - 推荐过,看上去有点难看,但有效

Using attribute selectors - recommended too, looks a bit ugly but effective.

我在这里看到了几个问题,担心这些选择的表现。的这是最好的方式?的第

I have seen a few questions here, worrying about performance with these selectors. Is this the best way possible? No.

不过,你们中的大多数甚至不会注意到性能损失,当然,除非你的DOM树是巨大的时间。

But, most of the time you won't even notice the performance hit, unless of course, your DOM tree is huge.

2选项:

Option3:

使用的CssClass - 强烈推荐。因为使用类选择干净,简单。

Using CssClass - highly recommended. Because selectors using classes are clean and uncomplicated.

在如果你想知道,的CssClass 对于.NET控件是一样的传统的HTML控件。

In case you are wondering, CssClass for .net controls is the same as class for traditional html controls.

<asp:TextBox runat="server" ID="myTextBox" CssClass="myclass" /> //add CssClass

$('.myclass') //selector

选项4:

Option4:

使用的ClientIDMode =静态,它得到了在.NET框架4.0中引入的,在控制,因此,它的ID将保持不变。 - 推荐过

Use ClientIDMode="Static", which got introduced in .NET Framework 4.0, on the control so that it's ID will stay unchanged. - recommended too.

<asp:TextBox runat="server" ID="myTextBox" ClientIDMode="Static"  /> //add ClientIDMode

$('#myTextBox') //use the normal ID selector

注意:
根据我的经验,我已经看到了丑陋的选择,比如 $('#ctl00_Main_myTextBox')。这是直接复制粘贴从网页呈现的ID和在脚本用它的结果。你看,这会奏效。但想想如果控件ID或主ID的变化会发生什么。很明显,你将不得不重新审视这些ID,并再次更改。取而代之的是,使用上述选项之一,覆盖。

Note: In my experience, I have seen ugly selectors like $('#ctl00_Main_myTextBox'). This is the result of directly copy pasting the ID rendered from the page and use it in the script. Look, this will work. But think about what will happen if the control ID or the master ID changes. Obviously, you will have to revisit these IDs and change them again. Instead of that, use one of the options above and be covered.

这篇关于访问Asp.net控制使用jQuery(所有选项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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