• 多种方法实现超长字符用"....."代替


    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样式来控制;


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mane_yao/archive/2010/07/20/5750443.aspx

  • 相关阅读:
    ssdb使用笔记
    跟我学爬虫-2-使用正则表达式解析文本
    跟我学爬虫-1-爬虫简介
    python int函数转换浮点型字符串的坑???
    python使用smtplib和email发送腾讯企业邮箱邮件
    php文件之间传值的三种主流并且常用的方式
    验证码的输入框与图片不能对齐问题
    web前端命名规范
    css盒子模型
    css基础
  • 原文地址:https://www.cnblogs.com/mane/p/1829939.html
Copyright © 2020-2023  润新知