What's the best way to deal with ASP.NET's ClientID

不问归期 提交于 2019-12-01 11:24:57

Why not just use ClientIDMode=Static? Set it in the Web.config so that your client IDs will be as-written throughout the site.

If you are using .Net framework 4.0 or higher ,Use the ClientIDMode property of the Control

You may use Static as the ClientIDModeEnumeration

The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.

You can use the jQuery ends with selector:

$('input[id$="myServerId"]')

But remember, that kind of selector is a lot slower than selecting directly with the id with <%= id.ClientID %>.

http://jsperf.com/id-vs-ends-with

The right answer is as follow

var txt1 = $('#<%= txt1.ClientID %>');
$(txt1).click(function () {
    alert('hi');
});

her I create a new variable on the javascript which is reference to the control based on its rendered client ID

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!