Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to prevent master pages from changing element IDs?

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_).

Unforunately, this breaks existing scripts on the page that use the original, unmodified element ID.

I realize that I can replace it with <%= Element.ClientID %> but it'd be awesome if I could disable this behavior altogether.

So, can I keep the original IDs?

like image 696
Michael Haren Avatar asked May 10 '09 02:05

Michael Haren


3 Answers

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
like image 134
Jose Basilio Avatar answered Nov 10 '22 12:11

Jose Basilio


You can override ClientID and UniqueID in the controls. This is from here, an article by Rick Strahl.

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

public override string ClientID
{   
    get
    {
        return this.ID;
    }
}
like image 36
Phaedrus Avatar answered Nov 10 '22 10:11

Phaedrus


This question is old as dirt, but the new way to do it is ClientIDMode="Static".

like image 2
Barry Franklin Avatar answered Nov 10 '22 11:11

Barry Franklin