Don’t you just hate it when the VS.Net HTML editor mangles your ASP. Net template code, because it thinks it knows better? Right!
In my Photo Album I have some admittedly ugly code that deals with sizing of a DIV tag that holds the thumbnail images. The page is supposed to be easily customizable for each photo album so the page contains a bunch of embedded server tags instead of using code behind to set values on a RUNAT server control.
Here’s what the tag looks like (the style string is all on one line – broken here for readability):
<div id="ThumbNailContainer"
style='OVERFLOW:auto;height:<%=this.ThumbNailDisplayColumnHeight%>;
width:<%= this.ThumbNailDisplayColumnWidth%>'
nowrap >
This WORKS just fine when saved and run. The value is properly inserted into the output.
However, if I make a change to the document and resave it, VS eats the height and width style tags turning the above into:
<div id="ThumbNailContainer"
style='OVERFLOW:auto;'
nowrap >
Thinking I would be smart about it I thought I’d create a style tag that contains the above styles and then use a class=’ThumbNailContainerStyleClass’. VS.Net did the same damn thing to my style definition, stripping out the width and height with the embedded values.
In the end I could see only two ways to fix this: Using some client side script or using a Server control and setting the width and height programmatically from code. I used the latter… To do so I added the runat="server" tag:
<div id="ThumbNailContainer" runat="server" style='OVERFLOW: auto;' nowrap>
You have to manually add any HTML controls to the form so that the code sees it in the code behind like this:
protected System.Web.UI.HtmlControls.HtmlContainerControl ThumbNailContainer;
Then inside the Page_Load code you can now set the style properties like this:
// *** Figure out and embed the width and height of the DIV tag
// *** that hosts the thumbnails
ThumbNailContainer.Style.Add("width",this.ThumbNailDisplayColumnWidth);
ThumbNailContainer.Style.Add("height",this.ThumbNailDisplayColumnHeight);
Now it all works, but what a hassle just because the editor reformats my HTML…
This is probably my #1 complaint I have with building ASP.Net code inside of VS – it always screws with the HTML turning it into a jumbled mess. This is the first time though I’ve run into a situation where it’s not just a esoteric problem, but where the editor actually mangled my logic which is a major problem.
Another frequent problem I have is when you use server tags or databinding tags embedded inside of strings. The workarounds there are sometimes even worse. The funny part is that the forms run just fine, but you cannot get the editor to render the WYSIWYG view.
Whidbey supposedly addresses this and from what limited tests I've done it looks like this is definitely holding true. Imagine that an editor from Micorosoft that doesn't touch your code?