Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 871
Mafti Avatar asked May 22 '09 13:05

Mafti


People also ask

What does runat server do?

The runat="server" tag in ASP.NET allows the ability to convert/treat most any HTML element as a server-side control that you can manipulate via code at generation time. Some controls have explicit implementations, others simply revert to a generic control implementation.

Does adding a runat server attribute to an HTML element change anything?

HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, add a runat="server" attribute to the HTML element. This attribute indicates that the element should be treated as a server control.

What is form runat server?

The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts. In the following example we declare an HtmlAnchor server control in an .aspx file.

What is runat server in C#?

Runat='Server ' Indicates the accessibility of the control at Serverside. Let Me make you more clear about it. If you puts runat="server" inside any of the control then you can use that control at the server side. e.g. XML.


1 Answers

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>
like image 60
Tristan Warner-Smith Avatar answered Oct 02 '22 15:10

Tristan Warner-Smith