ASP.NET Gridview超出长度用..代替,使用tooltip(title)提示显示详细信息
ASP.NET Gridview单元格字段值超出固定长度用.....代替,并使用TOOLTIP显示详细信息的实现方法很多,以下列举三种。
http://blog.csdn.net/chinajiyong/article/details/7389998
第一种:
在Gridview的事件RowDataBound中添加如下代码:
- for (int i = 0; i < e.Row.Cells.Count; i++)//获取总列数
- {
- //如果是数据行则添加title
- if (e.Row.RowType == DataControlRowType.DataRow)
- {//设置title为gridview的head的text
- e.Row.Cells[i].Attributes.Add("title", Gridview1.HeaderRow.Cells
- [i].Text.ToString().Trim());
- }
- }
- }
- if (e.Row.RowIndex > -1)
- {
- string tmp =((HyperLink) e.Row.FindControl("HypLinkTitle")).Text;
- if (tmp.Length > 3)
- {
- ((HyperLink)e.Row.FindControl("HypLinkTitle")).Text = tmp.Substring(0, 3) + "…";
- ((HyperLink)e.Row.FindControl("HypLinkTitle")).Attributes.Add("title", tmp);//以tooptip的方式显示全部内容
- }
- }
第二种:用三元运算符!截取字符串,当超过你指定的长度后截取,绑定时候用三目运算处理。代码如下:
- <%# Eval("title").ToString().length>100?Eval("title").ToString().length(0,100)+"...":Eval("title").ToString()%>
第三种:最简单的可以通过CSS来设置省略号,鼠标移上全部显示用ToolTip,分3步来实现,代码如下:
1.设置CSS
- <style type="text/css">
- .mlength
- {
- display: block;
- width: 100px;
- overflow: hidden;
- white-space: nowrap;
- -o-text-overflow: ellipsis;
- text-overflow: ellipsis;
- }
- </style>
2.设置GridView的某一字段为模板列并设置css,如:
- <asp:Label ID="Label1" runat="server" Text='<%# Eval("字段") %>' CssClass="mlength"></asp:Label>
3.后台设置ToolTip:
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- ((Label)e.Row.Cells[0].FindControl("Label1")).ToolTip = ((Label)e.Row.Cells[0].FindControl("Label1")).Text;
- }
- }
当然,ToolTip也可以在前台设置,代码如下:
- <asp:Label ID="Label1" runat="server" Text='<%# Eval("字段") %>' CssClass="mlength" ToolTip='<%# Eval("字段") %>'></asp:Label>

浙公网安备 33010602011771号