Need workaround for .Net Master Page Name Mangling

让人想犯罪 __ 提交于 2019-12-20 01:01:56

问题


I'm evaluating converting an old frameset based asp.net website to use master pages. The only thing holding me back is the huge amount of work it will take to update every page to deal with name mangling. Most of my problems are with javascript referencing hardcoded Id's.

Is there a way for me to tell ASP.Net that for a particular content area that I don't want mangling to occur. Leave it to me deal with name conflicts.

Note

I'm aware .Net 4.0 has a solution for this as detailed here. I want a solution that doesn't involve waiting, needs to be .Net 3.5.

Update

Any suggestions for opensource alternatives to masterpages that will get me by until .Net 4.0? Or how about a hack job solution to work around the mangling. Thanks


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1736867/need-workaround-for-net-master-page-name-mangling

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