如何在使用MasterPage的ASP.NET Web窗体中为服务器控件设置特定的ID? [英] How to set specific ID for server controls in an ASP.NET Web Form that is using a MasterPage?
问题描述
是否可以在ASP.NET服务器控件上设置特定的ID?每次我分配一个ID并运行Web表单时,ID都会更改.
Is it possible to set a specific ID on an ASP.NET server control? Everytime I assign an ID and run the web form the ID changes.
例如:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
被翻译成这样:
<input id="ct100_ContentPlaceHolder1_txtName" type="text" />
我认为这是使用母版页完成的,但是如果是这样,我如何才能确保控件具有特定的ID(出于JavaScript的目的).我将自动生成的ID放在了javascript中,并且可以正常工作,但是我希望使用我最初为其分配的ID.这可能吗?
I think this is do to me using master pages, but if so how can I be sure a control will have a certain ID(for javascript purposes). I placed the auto-generated id in my javascript and it is working, but I would prefer to have used the id's that I originally assigned them. Is this possible?
(适用于版本:ASP.NET 3.5)
(This is for version:ASP.NET 3.5)
推荐答案
这是Web控件ID在4.0版之前的.NET中的方式.版本4.0引入了客户端ID,您可以阅读
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.
您可以在JS中使用类似的东西:
You can use somthing like this in your JS:
var something = '<%= txtName.ClientID %>';
这篇关于如何在使用MasterPage的ASP.NET Web窗体中为服务器控件设置特定的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!