Wednesday, October 15, 2014

If you migrate what was in a web form without a master page into a web form that uses a master page, expect the ids of HTML tags rendered to change up.

If you have Default.aspx in the root of a web forms application standalone and it has a asp:Label inside like so:

<asp:Label runat="server" ID="foo">Test</asp:Label>

 
 

The HTML that will end up at the browser will look like this:

<span id="foo">Test</span>

 
 

Yet, when you recreate Default.aspx as a web form which uses a master page and put the same label in it the HTML might look like this:

<span id="MainContent_foo">Test</span>

 
 

This would come from the asp:Label being nested within an asp:Content tag with a ContentPlaceHolderID of "MainContent." This situation dirties up the ids and is likely to sabotage JavaScript that had been working just fine before you decided you had to get that old page from another era into the master page like all of the newer pages. The way to get the HTML back to this shape...

<span id="foo">Test</span>

 
 

...is to doctor your tags like so:

<asp:Label runat="server" ID="foo" ClientIDMode="Static">Test</asp:Label>

 
 

Make sense?

No comments:

Post a Comment