首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >适合GridView列中的长文本

问适合GridView列中的长文本
EN

Stack Overflow用户
提问于 2013-07-16 23:09:47
回答 3查看 6.5K关注 0票数 0

我在ASP.NET网格视图列中显示长文本时遇到问题。我不想让文本换行到第二行,因为它是业务需求中不能换行的内容之一。

理想情况下,我需要一些基于服务器或客户端的代码,这些代码可以帮助我将文本截断到列的大小,然后可能会显示一个更多按钮或“...”想要更多文本吗?

当更多按钮或..。将显示一个弹出窗口,其中包含文本的其余部分或完整文本。

注意:文本或字符串大小各不相同,可以是25到75个字符之间的任意长度。

对于如何实现上述目标,您有什么建议吗?谢谢

EN

回答 3

Stack Overflow用户

发布于 2013-07-17 01:18:29

将此CSS类用于gridview列

代码语言:javascript
复制
.ellipsis {
    white-space: nowrap;
    text-overflow: ellipsis;
    width: 50px;
    display: block;
    overflow: hidden;
}

它把..。在特定宽度的列的末尾。

更新

标记

代码语言:javascript
复制
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
        AutoGenerateColumns="False" CssClass="employees-grid">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="ID">
                <ItemStyle CssClass="employees-grid-id" />
            </asp:BoundField>
            <asp:BoundField DataField="first_name" HeaderText="First Name">
                <ItemStyle CssClass="employees-grid-first-name" />
            </asp:BoundField>
            <asp:BoundField DataField="last_name" HeaderText="Last Name">
                <ItemStyle CssClass="employees-grid-last-name" />
            </asp:BoundField>
            <asp:BoundField DataField="email" HeaderText="Email">
                <ItemStyle CssClass="employees-grid-email" />
            </asp:BoundField>
            <asp:BoundField DataField="note" HeaderText="Note">
                <ItemStyle CssClass="employees-grid-note" />
            </asp:BoundField>
        </Columns>
</asp:GridView>

CSS

代码语言:javascript
复制
.employees-grid {
    table-layout: fixed;
    width: 100%;
}
.employees-grid-id {
    width: 5%;   
}
.employees-grid-first-name {
    width: 10%; 
}
.employees-grid-last-name {
    width: 10%;   
}
.employees-grid-email {
    width: 15%; 
}
.employees-grid-note {
    width: 100%;
    white-space: nowrap;
    text-overflow: ellipsis;
    display: block;
    overflow: hidden;
}
票数 5
EN

Stack Overflow用户

发布于 2013-07-16 23:52:19

如果您关心格式化,我将构建我的查询(您将GridView绑定到的查询),以便以两种形式返回数据。

代码语言:javascript
复制
Select Left(dataColumn, 20) as TruncatedData, dataColumn as FullData From table

然后,您可以将GridView绑定到TruncatedData列,并将工具提示绑定到FullData列,这样当它们悬停时,它们就会获得完整的列数据。我会将BoundField转换为TemplateField,并填充如下内容:

代码语言:javascript
复制
          <asp:templatefield>
              <itemtemplate>
                  <asp:label id="lblData"
                      Text= '<%# Eval("TruncatedData") %>'
                      runat="server"
                      ToolTip='<%# Eval("FullData") %>' /> 
              </itemtemplate>
          </asp:templatefield>
票数 0
EN

Stack Overflow用户

发布于 2013-07-16 23:57:07

你应该发布你已经尝试过的代码,这样人们就可以提出修改/改进等建议。在任何情况下,我都不得不写一段代码来做一些关于你正在寻找的东西的事情,所以这里你有它。

代码语言:javascript
复制
private string[] truncateText(string iniText, int colNumber, int noAddRows)
{
    string[] outStrings = new string[noAddRows + 3];
    if (noAddRows == 0)
    {
        outStrings = new string[22];
    }
    outStrings[1] = iniText;
    int addBit = 10;
    int maxLength = (int)GridView1.Columns[colNumber].ItemStyle.Width.Value + addBit;
    int linesCount = 1;

    if (iniText.Length > maxLength)
    {
        outStrings[1] = iniText.Substring(0, maxLength); //New truncated string
        outStrings[2] = iniText.Substring(maxLength, iniText.Length - maxLength); //Remaining bit
        linesCount = 2;
        if (noAddRows > -1 && outStrings[2].Length > maxLength)
        {
            //Further lines of the truncated bit
            string remBit = outStrings[2].Substring(maxLength, outStrings[2].Length - maxLength);
            outStrings[2] = outStrings[2].Substring(0, maxLength);
            while (remBit.Length > 0)
            {
                linesCount = linesCount + 1;
                if ((noAddRows > 0 && linesCount > noAddRows + 2) || linesCount > 20)
                {
                    linesCount = linesCount - 1;
                    if (remBit.Length > 0)
                    {
                        outStrings[linesCount] = outStrings[linesCount] + remBit;
                    }
                    break;
                }

                if (remBit.Length <= maxLength)
                {
                    outStrings[linesCount] = remBit;
                    break;
                }
                else
                {
                    outStrings[linesCount] = remBit.Substring(0, maxLength);
                    remBit = remBit.Substring(maxLength, remBit.Length - maxLength);
                }
            }
        }
    }

    outStrings[0] = Convert.ToString(linesCount); //Total number of lines

    return outStrings;
}

你可以这样称呼它:

代码语言:javascript
复制
string[] truncatedLines = truncateText("text to truncate", colNo, noAdd);

输出string[]保存所有被截断的行,以便它们适合给定列的宽度+一小部分(addBit,我将其设置为10)。使用noAddRows参数可以控制希望作为输出的行数:如果将其设置为零,它将根据给定的列宽返回所需的行数。数组的0位置保留为返回的总行数。

我猜你不会发现使用这段代码来完全控制你想要实现的功能有任何问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17680370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档