6

我正在考虑将母版页添加到现有站点,但发现一旦我这样做,元素的 ID 就会在前面加上代码(例如ctl00_MainPageContent_)。

不幸的是,这会破坏页面上使用原始、未修改元素 ID 的现有脚本。

我意识到我可以将其替换为,<%= Element.ClientID %>但如果我可以完全禁用此行为,那就太棒了。

那么,我可以保留原始ID吗?

4

3 回答 3

5

上一篇文章已经回答了这个问题:删除 MasterPage Generated ID

该解决方案使用以下代码覆盖渲染事件:

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
于 2009-05-10T02:53:47.097 回答
4

您可以在控件中覆盖 ClientID 和 UniqueID。这是来自这里,Rick Strahl 的一篇文章。

public override string UniqueID
{
    get
    {
        return this.ID;
    }
}

public override string ClientID
{   
    get
    {
        return this.ID;
    }
}
于 2009-05-10T03:12:02.887 回答
1

这个问题像泥土一样古老,但新的方法是ClientIDMode="Static"

于 2019-10-10T20:31:42.263 回答