在GridView中帮顶列后,有时某一列的长度太长,影响美观。所以就要进行适当的截取。这里介绍两种情况下的截取方法:
1,如果是直接通过BoundField帮顶。只需要在RowDataBound事件中。通过e.rows.cells[i].text来得到值。
2,如果是模板列就要在模板列中放label, 如下:
<asp:TemplateField HeaderText="内容">
<ItemTemplate>
<asp:Label ID="txtContents" runat="server" Text='<%# Eval("contents") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
在RowDataBound事件中这样写就可以得到:
Label txt = (Label)e.Row.FindControl("txtContents");
if ( txt.Text.Length>15)
{
txt.Text = txt.Text.Substring(0,15)+"...";
}
这样就oK了。
本文介绍了在ASP.NET的GridView中如何处理过长的列显示问题,提供了两种实用的方法:一是直接通过BoundField进行处理;二是使用模板列结合Label组件实现。这两种方法均可在RowDataBound事件中对显示文本进行截取,确保列宽适中且信息展示清晰。
1731

被折叠的 条评论
为什么被折叠?



