多种方法实现超长字符用"....."代替
2010-07-20 17:52
429 查看
1、gridview
公用方法:
protected string Intercept(string sInput)
{
if (sInput != null && sInput != string.Empty)
{
if (sInput.Length > 10)
return sInput = sInput.Substring(0, 10) + "...";
else
return sInput;
}
return "";
}
第一种方法:在GridView的RowDataBound方法中遍历每一行数据;
protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
string sGUID;
if (e.Row.RowType == DataControlRowType.DataRow)
{
sGUID = e.Row.Cells[0].Text;
//display the changed values
e.Row.Cells[0].Text = Intercept(sGUID);
}
}
第二种方法:将GridView的某一列(改变默认显示)变成模板列;
具体步骤:GridView右键-显示智能标记-编辑列-在选定的字段里面选中字段(右下角点击将此字段转换为TemplateField)
(如果你从第一个方法试到第二个方法,第二个方法完成后运行,你会发现编号这行没有数据显示,知道为什么吗?)
答案就是第一种方法里面已经改变了显示效果(cs代码),如果你再次在html代码调用Intercept(...)方法将return "",所以没有数据显示;
*注意:这里将某一列变成了模板列的时候,Html代码绑定该字段是:Text='<%# Bind("GUID") %>',这里若要运用上面的公用方法(Intercept(...)),将Bind改成Eval,具体代码请看:
<asp:TemplateField HeaderText="编号">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("GUID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Intercept(Eval("GUID").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
第三种方法最简单:通过CSS样式来控制;
在html中加入样式表
<style type="text/css">
.ellipsis
{
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
</style>
<asp:Label ID="Label1" runat="server" CssClass="ellipsis" Width="80px" Text='<%# Bind("GUID") %>'></asp:Label>
相信大多数人会喜欢这种,不用编写代码;
2.DataList
第一种方法:在ItemDataBound(...)方法中遍历所有行
protected void dlMain_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblGUID = (Label)e.Item.FindControl("lblGUID");
System.Data.Common.DbDataRecord dbdr = (System.Data.Common.DbDataRecord)e.Item.DataItem;
//这里可以改变任何一列的数据显示
string sGUID = Convert.ToString(dbdr["GUID"]);
lblGUID.Text = Intercept(sGUID);
}
}
第二种方法:也是通过CSS样式来控制;
公用方法:
protected string Intercept(string sInput)
{
if (sInput != null && sInput != string.Empty)
{
if (sInput.Length > 10)
return sInput = sInput.Substring(0, 10) + "...";
else
return sInput;
}
return "";
}
第一种方法:在GridView的RowDataBound方法中遍历每一行数据;
protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
string sGUID;
if (e.Row.RowType == DataControlRowType.DataRow)
{
sGUID = e.Row.Cells[0].Text;
//display the changed values
e.Row.Cells[0].Text = Intercept(sGUID);
}
}
第二种方法:将GridView的某一列(改变默认显示)变成模板列;
具体步骤:GridView右键-显示智能标记-编辑列-在选定的字段里面选中字段(右下角点击将此字段转换为TemplateField)
(如果你从第一个方法试到第二个方法,第二个方法完成后运行,你会发现编号这行没有数据显示,知道为什么吗?)
答案就是第一种方法里面已经改变了显示效果(cs代码),如果你再次在html代码调用Intercept(...)方法将return "",所以没有数据显示;
*注意:这里将某一列变成了模板列的时候,Html代码绑定该字段是:Text='<%# Bind("GUID") %>',这里若要运用上面的公用方法(Intercept(...)),将Bind改成Eval,具体代码请看:
<asp:TemplateField HeaderText="编号">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("GUID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Intercept(Eval("GUID").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
第三种方法最简单:通过CSS样式来控制;
在html中加入样式表
<style type="text/css">
.ellipsis
{
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
</style>
<asp:Label ID="Label1" runat="server" CssClass="ellipsis" Width="80px" Text='<%# Bind("GUID") %>'></asp:Label>
相信大多数人会喜欢这种,不用编写代码;
2.DataList
第一种方法:在ItemDataBound(...)方法中遍历所有行
protected void dlMain_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblGUID = (Label)e.Item.FindControl("lblGUID");
System.Data.Common.DbDataRecord dbdr = (System.Data.Common.DbDataRecord)e.Item.DataItem;
//这里可以改变任何一列的数据显示
string sGUID = Convert.ToString(dbdr["GUID"]);
lblGUID.Text = Intercept(sGUID);
}
}
第二种方法:也是通过CSS样式来控制;
相关文章推荐
- 多种方法实现超长字符用"....."代替
- 多种方法实现超长字符用"....."代替
- "找出字符串中第一个不重复的字符"和"找出字符串中最后一个重复的字符"的两种方法的C++实现
- windowsxp下三种实现"忽略驱动程序签名"的方法
- 10_9_2从类MyClass中派生一个类MyDervedClass,重写GetString(),使用该方法的基类实现代码从基类中返回一个字符串,但在返回的字符串中添加文本"(output fro)"
- 如何实现在main()方法执行前输出"Hello World"?
- Java连接MySQL数据库实现用户名密码的验证方法 Java语句中sql查询语句'' ""作用
- 实现src="***.do",参数通过其他方法传!
- 关于JSP中单击任意标签弹出文件选择框(<input type="file"/>)的实现方法
- 如何连接判断字符新方法,如:where id0 and username="" and ......2006-6-8-3
- 请输出开如"1A 2B 3C ........26Z"的字符序列,用集合实现
- 多种方法运用“...”代替超长字符串(gridview、datalist)
- 实现左旋字符串中前k个字符的多种方法(交换法、双倍字符串法、翻转)
- as3中通过命名空间来实现方法的 "重载"
- "密码最短长度为7,其中必须包含以下非字母数字字符1"解决方法
- 项目请求"进度条"实现方法
- "路径和文件名总长度必须不能超过 260 个字符! 系统找不到指定的路径"的解决方法
- 编程实现:从字符中获取连续数组序列,如字符串"a1dl2iad9j3la5kudp7u9pn4blj8ap5u3e6ml9a"中可以得到的连续数字序列为1234556.【注】:只考虑一位,因此最长的数
- 多种方法运用“...”代替超长字符串(gridview、datalist)
- 编写一个业务函数,实现字符串(前后各有三个空格,单词前后也均有空格) " i am student, you are teacher " , 各个单词首字符大写