【问题标题】:ASP.NET - Get DIV width and set Table widthASP.NET - 获取 DIV 宽度并设置表格宽度
【发布时间】:2013-04-01 22:04:42
【问题描述】:

我有一个 ASP.NET、C# Web 应用程序,它的 DIV 标记设置为 100%,因此 div 的内容可以根据内容扩展或缩短。这很好用,但我想修改它,以便如果内容低于某个宽度,则将宽度设置为标准宽度。因此,对于仅返回包含 2 列且数据不多的表的页面,这将消除看起来非常小的 DIV 标记。

我设置了 DIV 和表格的 ID 和 runat 属性,但是当我尝试通过单击页面上的按钮来获取宽度时,它总是返回 null:

<div id="content" runat="server">
   <table class="MainContent" width="100%" id="MainTable" runat="server">
        'data is in this table that could be 2 columns or 20, it's loaded 
        'dynamically in a gridview
   </table>
</div>

我的按钮代码:

string s = content.Attributes["width"];

如何在我的网络应用程序中执行此操作?一、仅限使用ASP.NET 2.0和.NET 3.5框架。

【问题讨论】:

  • 服务器不知道 div 的宽度。这是由客户决定的。
  • @JohnPick - 谢谢...我确实找到了基本 html 的解决方法:
    内表/div

标签: asp.net html


【解决方案1】:

使用 CSS:

min-width

定义和用法

The min-width property sets the minimum width of an element. Note: The min-width property does not include padding, borders, or margins!

【讨论】:

    【解决方案2】:

    您可以在代码隐藏中使用此代码...

    <<div_name_here>>.Style.Add("property", "setting");
    

    例如

    content.Style.Add("width", "600px");
    

    支持 CSS 样式。

    【讨论】:

      猜你喜欢
      • 2011-02-26
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多