添加 runat="server"改变布局的行为 [英] adding runat="server" changes the behaviour of the layout

查看:30
本文介绍了添加 runat="server"改变布局的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些控件、用户控件等的页面

当我将 div 从普通 <div id="foo"> 更改为 <div id="foo" runat="server"> 时>布局完全改变.

为什么会这样,我该如何预防?

我使用的是 2.0 .NET 框架

是不是因为 .NET 改变了我的 id,这显然是我不想要的?

解决方案

如果您在 CSS 中定位 div 控件的 ID,然后在服务器上运行该控件,您会发现它不再应用该样式.

这是因为 ASP.NET 有一个内置机制 (INamingContainer) 来确保您没有多个命名相同的控件.它通过添加容器前缀来实现这一点,因此您最终得到:

解决此问题的最简单方法是将其从处理 ID 更改为处理类:

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

或者,我相信 XHTML 要求 Divs 有结束标记,所以使用

<div runat="server">一些内容</div>

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?

解决方案

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>

这篇关于添加 runat=&amp;quot;server&amp;quot;改变布局的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆