GridView中的每个单元格文本长度的控制,鼠标悬停时,显示单元格所有的内容

来源:互联网 发布:js中获取radio的值 编辑:程序博客网 时间:2024/05/16 18:25
怎样实现GridView中的每个单元格文本长度的控制,鼠标悬停时,显示单元格所有的内容,方法以下:
1)在.aspx页面GridView控件添加OnDataBound属性,如下所示:
<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound"></asp:GridViw>
2)在后台.aspx.cs文件GridView1_DataBound事件函数添加以下代码:
view source
print?
01privatevoidGridView1_RowDataBound(objectsender, GridViewRowEventArgs e)
02{
03   //判定当前类型是否为数据行,如果是,则添加title
04   if(e.Row.RowType == DataControlRowType.DataRow)
05   {
06       //获取列数,进行循环添加title
07       for(inti=0;i<e.Row.Cells.Count;i++)
08       {
09           //定义一个string类型变量用来存放每个单元格的内容
10           stringtemp = e.Row.Cells[i].text;
11           //设置title为GridView的HeadText
12           e.Row.Cells[i].Attributes.Add("title",temp);//未截取长度   
13           //判定temp的长度,
14           if(temp.length>10)
15           {
16             //截取字符串
17             temp = temp.SubString(0,9)+"...";
18           }
19       }       
20             
21       
22   }
23 
24}
0 0