Need workaround for .Net Master Page Name Mangling

南笙酒味 提交于 2019-12-01 20:11:36

The only "supported" way to do this is to NOT use elements that are defined as "runat="server"". Otherwise, .NET 4.0 is the first time that you are given a consistent, supported mechanism to make this change.

You might be able to get around this via other means, but nothing that is going to be easy/quick to implement.

One way to do this is with some server-generated JavaScript that associates the generated ID with a more friendly name. You can access the generated ID using the ClientID property. For example:

<asp:Label runat="server" ID="myInfo" Text="Initial text" />    
<script type="text/javascript">
  function RegObj(clientId, anId) {
    eval('window.' + clientId + ' = document.getElementById(anId)');
  }
  RegObj('mytext', '<%= myInfo.ClientID %>');
  mytext.innerHTML = 'my text';
</script>

This reported ID includes any mangling done by Master Pages, nested controls, etc.

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