【问题标题】:ASP.NET - Get GridView width after data is boundASP.NET - 绑定数据后获取 GridView 宽度
【发布时间】:2012-02-18 02:19:36
【问题描述】:

我正在尝试做一些相当简单的事情。我有一个 div 标签,其中显示了我的 gridview。我想将 div width 属性设置为 gridview 的 width 属性 + 更多像素。我试图访问 DataBound 和 Page_Prerender 事件中的 GridView.Width.Value 属性,但它总是返回 0:

protected void Page_Prerender(object sender, EventArgs e)
{
    string sWidth = gvUsers.Width.Value.ToString();
    divContactForm.Attributes.Add("style", "width: " + sWidth + " ;");
}

我在 DataBound 事件中尝试了相同的代码,但没有成功。

数据绑定后如何获取GridView的宽度?

【问题讨论】:

  • 我认为这在服务器端是不可行的,因为 GridView 的宽度取决于 CSS(字体大小、填充)和封闭的 DOM 对象(例如,窗口大小)。为什么不让你的 div 增长(不要设置宽度)?
  • 好吧,我确实设置了 div width: auto 但这会将 div 边框放在 gv 边框上。
  • 只需给 div 一些填充。例如:style="padding:5px 5px;"
  • 你试过设置div的width属性吗? divContactForm.Width = ... ?
  • @JohnPick - 这很有效,非常感谢。您能否将其发布为答案,以便我可以标记它,拜托。 subt13 - 这就是我最初在我的代码中尝试做的事情(见上文),但 GridView.Width 返回 0。

标签: asp.net


【解决方案1】:

只需给 div 一些填充。例如:

style="padding:5px 5px;"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    相关资源
    最近更新 更多