by aherrick
3. December 2008 06:34
I was working on a project and was looking for more control over the elements I was creating in the HTML. More specifically, I had a MasterPage setup and I wanted all pages to have the same background image throughout the site.
Simple. Add a runat=server to your markup. See below.
<div id="bgimg" runat="server">
</div>
You now have control of this element from the code-behind.
To add a background image to the div, you could do something like this:
bgimg.Style[HtmlTextWriterStyle.BackgroundImage] = ResolveUrl("~/images/bodyback.jpg");
The only thing to realize is what your generated markup is. Asp.Net will auto-generate this tag to prevent naming collisions. Just look out for it if you are trying to style the element with CSS or call it using javascript.
<div id="ctl00_bgimg" style="background-image:url(/HRR/images/bodyback.jpg);">
</div>
Enjoy!