GridView 生成的代码添加了“width=”100%“”到它创建的表
我的页面在 Firefox 和 IE8 中看起来正确。但在 IE7 中,嵌套的 gridview 溢出到相邻的单元格中,就像问题
在开发人员工具中查看,有一个与 ASP.NET 生成的表关联的内联样式,并且它的宽度属性为 100%。如果我删除它,嵌套表会弹回到它所属的位置。
问题是,没有任何地方是内联样式集。事实上,如果我尝试设置 width='250px'
,它会被 width='100%'
覆盖。如果我尝试删除代码隐藏中的宽度属性,attrGridView.Attributes["Width"]
为 null,并且调用 .Remove()
不会执行任何操作。但是每个 ASP.NET 生成的 gridview-table 都有一个内联样式,并在其上设置了 width='100%'
(它只在一个地方导致我出现问题)。
按照我链接到的文章中的建议设置 table-layout='fixed'
没有帮助。
如何让 ASP.NET 停止设置此属性?
一些代码:
<asp:TemplateField HeaderText="Attributes" SortExpression="Attributes">
<HeaderStyle CssClass="GridHeaderCell" />
<ItemStyle CssClass="GridTableCell AttrGridCellPadding" />
<ItemTemplate>
<asp:GridView id="attributesGridView" runat="server"
AutoGenerateColumns="false" ShowHeader="false" GridLines="None"
AlternatingRowStyle-BackColor="White" CssClass="StupidGridView" >
<EmptyDataTemplate>
<p class="italic">There are no attributes for this request.</p>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField>
<ItemStyle CssClass="AttrTableCell" />
<ItemTemplate>
<asp:Label id="attributeName" runat="server"
Text='<%# Eval("Name") + ": "+ Eval("Value") %>'
CssClass="AttrGridCell"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
.StupidGridView {
width: 250px;
}
My page looks correct in Firefox and IE8. But in IE7, a nested gridview spills into the adjacent cell, much like the issue here.
Looking at it in the developer tools, there is an inline-style associated with the table that ASP.NET generated, and it has a width attribute of 100%. If I remove this, the nested table pops back where it belongs.
Problem is, nowhere is an inline-style set. In fact, if I try to set width='250px'
, it gets overridden with width='100%'
. If I try to remove the width attribute in the code-behind, attrGridView.Attributes["Width"]
is null, and calling .Remove()
does nothing. But every asp.net-generated gridview-table has an inline style with width='100%'
set on it (it's only causing me issues in one place).
Setting table-layout='fixed'
, as suggested in the article I linked to, did not help.
How do I get ASP.NET to stop setting this property?
Some code:
<asp:TemplateField HeaderText="Attributes" SortExpression="Attributes">
<HeaderStyle CssClass="GridHeaderCell" />
<ItemStyle CssClass="GridTableCell AttrGridCellPadding" />
<ItemTemplate>
<asp:GridView id="attributesGridView" runat="server"
AutoGenerateColumns="false" ShowHeader="false" GridLines="None"
AlternatingRowStyle-BackColor="White" CssClass="StupidGridView" >
<EmptyDataTemplate>
<p class="italic">There are no attributes for this request.</p>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField>
<ItemStyle CssClass="AttrTableCell" />
<ItemTemplate>
<asp:Label id="attributeName" runat="server"
Text='<%# Eval("Name") + ": "+ Eval("Value") %>'
CssClass="AttrGridCell"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
.StupidGridView {
width: 250px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正在应用主题并覆盖控制级别设置。检查页面、web.config 或可能设置主题的其他任何位置上的主题设置。
Themes are being applied and overriding control-level settings. Check theme settings on page, web.config, or anywhere else a theme may be set.
不幸的是,ASP.NET html 渲染非常糟糕。
Microsoft 知道这一点,并在 2006 年提供了控件适配器,允许您修改控件渲染。
我建议使用 CSSFriendly ,而不是搜索如何覆盖 ASP.NET 渲染,它提供了控制适配器大多数 ASP.NET 呈现错误的控件。
如果你不需要“纯 CSS 渲染”并且 CSS 害怕你,你可以检查他们如何创建你自己的适配器。
ScottGu 发表于此主题来自 Google。
Unfortunately, the ASP.NET html rendering is really bad.
Microsoft know that and provided Control adapters in 2006 which allow you to modify control rendering.
Instead of searching on how to override what ASP.NET render, I would advise to use CSSFriendly which provides control adapters for most ASP.NET bad-rendered controls.
If you don't need "pure-css rendering" and CSS afraid you, you can check how they do to create your own adapter.
ScottGu post on this subject from google.