How to set specific ID for server controls in an ASP.NET Web Form that is using a MasterPage?

≡放荡痞女 提交于 2019-12-04 03:23:49

This is the way web controls ID's are in .NET prior to version 4.0. Version 4.0 introduces client IDs, which you can read about here.

You can use somthing like this in your JS:

var something = '<%= txtName.ClientID %>';

Starting with .NET 4 you have greater control about how the client-side IDs look like (see this post for details).

To force a specific client-side ID, you have to set the ClientIDMode to static. The following will render an <input> element with id="txtName":

<asp:TextBox ID="txtName" ClientIDMode="static" runat="server"></asp:TextBox>

Although if you do this, you have to ensure that you don't have two controls with identical client-side IDs. Check the article linked above for other options.

You can use the Control.ClientID property in your codebehind to get the actual id after it's been added to the control tree.

Super annoying choice made by the asp.net webforms people.

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