C# 截断ASP.NET GridView中的文本

C# 截断ASP.NET GridView中的文本,c#,asp.net,C#,Asp.net,以下是GridView,它显示数据库中表中的所有列和记录: <asp:GridView ID="GridView1" runat="server" AllowPaging="true" BackColor="White" BorderColor="#CCCCCC" BorderWidth="2px" CellPadding="2" CellSpacing="5" ForeColor="#000066" GridLines="None"> &l

以下是GridView,它显示数据库中表中的所有列和记录:

<asp:GridView ID="GridView1" runat="server" AllowPaging="true" BackColor="White" 
        BorderColor="#CCCCCC" BorderWidth="2px" CellPadding="2" CellSpacing="5"
        ForeColor="#000066" GridLines="None">
    <RowStyle BackColor="#F7F7F7" />
    <AlternatingRowStyle BackColor="#E7E7FF" />
    <FooterStyle BackColor="White" />
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="White" ForeColor="#000066" 
        HorizontalAlign="Center" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#007DBB" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#00547E" />
    <Columns>
        <asp:CommandField ShowSelectButton="true" HeaderStyle-ForeColor="Yellow"
            ControlStyle-ForeColor="Red" SelectText="Select" HeaderText="Select" />
    </Columns>
</asp:GridView>

在此表中,名为
Description
的列包含大量文本。在GridView中,我只想显示前10或20个字符,后跟。。。(三点)。当我将鼠标悬停在文本上时,我希望全文显示为工具提示。

有两种解决方案。解决方案在SQL端和代码端

第一名:

public static class StringHelper
{
    public static string Crop(this string text, int maxLength)
    {
        if (text == null) return string.Empty;

        if (text.Length < maxLength) return text;

        return text.Substring(0, maxLength);
    }
}
 <asp:TemplateField HeaderText="Description">               
           <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# StringHelper.Crop(Eval("Description").ToString(), 20) %>'></asp:Label>
           </ItemTemplate>
 </asp:TemplateField>
.wrappedText { word-wrap: break-word; }
从数据库获取数据时截断描述列值

SELECT SUBSTRING(Description, 0, 20) FROM Book
有关T-SQL中的
子字符串
函数的更多信息,请参阅


秒:

public static class StringHelper
{
    public static string Crop(this string text, int maxLength)
    {
        if (text == null) return string.Empty;

        if (text.Length < maxLength) return text;

        return text.Substring(0, maxLength);
    }
}
 <asp:TemplateField HeaderText="Description">               
           <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# StringHelper.Crop(Eval("Description").ToString(), 20) %>'></asp:Label>
           </ItemTemplate>
 </asp:TemplateField>
.wrappedText { word-wrap: break-word; }
您可以编写一个方法来裁剪字符串值并在
GridView
中使用它。不要忘记;为此,首先应将
Description
列字段转换为
TemplateField

助手类中的裁剪方法:

public static class StringHelper
{
    public static string Crop(this string text, int maxLength)
    {
        if (text == null) return string.Empty;

        if (text.Length < maxLength) return text;

        return text.Substring(0, maxLength);
    }
}
 <asp:TemplateField HeaderText="Description">               
           <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# StringHelper.Crop(Eval("Description").ToString(), 20) %>'></asp:Label>
           </ItemTemplate>
 </asp:TemplateField>
.wrappedText { word-wrap: break-word; }

请注意,命名为wrappedText的CSS类有两种解决方案。解决方案在SQL端和代码端

第一名:

public static class StringHelper
{
    public static string Crop(this string text, int maxLength)
    {
        if (text == null) return string.Empty;

        if (text.Length < maxLength) return text;

        return text.Substring(0, maxLength);
    }
}
 <asp:TemplateField HeaderText="Description">               
           <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# StringHelper.Crop(Eval("Description").ToString(), 20) %>'></asp:Label>
           </ItemTemplate>
 </asp:TemplateField>
.wrappedText { word-wrap: break-word; }
从数据库获取数据时截断描述列值

SELECT SUBSTRING(Description, 0, 20) FROM Book
有关T-SQL中的
子字符串
函数的更多信息,请参阅


秒:

public static class StringHelper
{
    public static string Crop(this string text, int maxLength)
    {
        if (text == null) return string.Empty;

        if (text.Length < maxLength) return text;

        return text.Substring(0, maxLength);
    }
}
 <asp:TemplateField HeaderText="Description">               
           <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# StringHelper.Crop(Eval("Description").ToString(), 20) %>'></asp:Label>
           </ItemTemplate>
 </asp:TemplateField>
.wrappedText { word-wrap: break-word; }
您可以编写一个方法来裁剪字符串值并在
GridView
中使用它。不要忘记;为此,首先应将
Description
列字段转换为
TemplateField

助手类中的裁剪方法:

public static class StringHelper
{
    public static string Crop(this string text, int maxLength)
    {
        if (text == null) return string.Empty;

        if (text.Length < maxLength) return text;

        return text.Substring(0, maxLength);
    }
}
 <asp:TemplateField HeaderText="Description">               
           <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# StringHelper.Crop(Eval("Description").ToString(), 20) %>'></asp:Label>
           </ItemTemplate>
 </asp:TemplateField>
.wrappedText { word-wrap: break-word; }

请注意,名为wrappedText的CSS类使用TemplateField,如下所示:

<asp:TemplateField HeaderText="Description">
    <ItemTemplate>
          <span title='<%#HttpUtility.HtmlEncode(Eval("Description").ToString)%>'><%#HttpUtility.HtmlEncode(Left(Eval("Description").ToString, 20)) + "..."%></span>
    </ItemTemplate>
</asp:TemplateField>


您可能希望在
说明
字段中检查并允许回车字符或单引号,因为这可能会破坏输出。

使用模板字段,如下所示:

<asp:TemplateField HeaderText="Description">
    <ItemTemplate>
          <span title='<%#HttpUtility.HtmlEncode(Eval("Description").ToString)%>'><%#HttpUtility.HtmlEncode(Left(Eval("Description").ToString, 20)) + "..."%></span>
    </ItemTemplate>
</asp:TemplateField>


您可能希望检查并允许在
描述
字段中使用回车字符或单引号,因为这可能会破坏输出。

谢谢,但如果我使用
模板字段
绑定描述字段,现在在浏览器上GridView显示两个描述文件,其中一个包含截断文本,另一个包含全文。其中一个描述字段位于范围的标题中,不应出现。如果出现,则说明中可能有一个单引号终止了title属性。尝试添加
。替换(“,”)
并查看发生了什么。@user2969489您确定gridview不生成自动列吗?我的答案可能是可以帮助您吗?谢谢,但如果我使用
TemplateField
Bind
描述字段,现在在浏览器上,GridView显示两个描述文件,一个是截断文本,另一个是全文。其中一个描述字段位于跨度的标题中,不应该出现。如果出现,则说明中可能有一个单引号终止了title属性。尝试添加
。替换(“,”)
并查看发生了什么。@user2969489您确定gridview不生成自动列吗?我的答案可能是,你能帮你吗?