有什么方法可以防止母版页更改元素 ID? [英] Any way to prevent master pages from changing element IDs?
问题描述
我正在考虑将母版页添加到现有网站,但发现一旦我这样做了,元素的 ID 就会预先添加一个代码(例如 ctl00_MainPageContent_
).
I'm looking at adding master pages to an existing site but have found that once I do, the elements' IDs get prepended with a code (e.g. ctl00_MainPageContent_
).
不幸的是,这会破坏页面上使用原始、未修改元素 ID 的现有脚本.
Unforunately, this breaks existing scripts on the page that use the original, unmodified element ID.
我意识到我可以用 <%= Element.ClientID %>
替换它,但如果我能完全禁用这种行为就太棒了.
I realize that I can replace it with <%= Element.ClientID %>
but it'd be awesome if I could disable this behavior altogether.
那么,我可以保留原始 ID 吗?
So, can I keep the original IDs?
推荐答案
这个问题已经在之前的帖子中得到了解答:删除 MasterPage 生成的 ID
The question was already answered in the previous post: Remove MasterPage Generated ID
解决方案使用以下代码覆盖渲染事件:
The solution overrides the Render Event with the following code:
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim Html As New StringWriter()
Dim Render As New HtmlTextWriter(Html)
MyBase.Render(Render)
writer.Write(Html.ToString().Replace("name=""ctl00$ContentBody$", _
"name=""").Replace("id=""ctl00_ContentBody_", "id="""))
End Sub
这篇关于有什么方法可以防止母版页更改元素 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!