Mafti Mafti - 1 year ago 64
ASP.NET (C#) Question

adding runat="server" changes the behaviour of the layout

I have a page with some controls, usercontrols etc.

when I change a div from plain

<div id="foo">
to a
<div id="foo" runat="server">

the layout complete changes.

why is that and how can I prevent it?

I'm using 2.0 .NET framework

Is it because .NET changes my id, which obviously I don't want?

Answer Source

If you're targetting the ID of the div control in CSS and then running the control at server, you'll find it no longer applies the style.

This is because ASP.NET has a built in mechanism (INamingContainer) to ensure than you don't have multiple controls named the same. It does this by adding container prefixes so you end up with:

<div id="ctl00_ctl00_myDivName" runat="server" />

The easiest way around this is to change it from working on an ID to working on a class:

<div class="myDiv" runat="server"></div>

Alternatively, I believe that XHTML requires that Divs have closing tags so use

<div runat="server">Some content</div>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download