怎么实现这种效果?

bj890 2008-06-24 05:04:16
GridView实现用“...”代替超长字符串
例如:
姓名 地址
yu 北京..
ta 上海..



<asp:GridView ID="GridView1" runat="server" Style="position: relative; left: 211px; top: 32px;" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="name" HeaderText="姓名" />
</Columns>
<Columns>
<asp:BoundField DataField="address" HeaderText="地址" />
</Columns>
</asp:GridView>
...全文
147 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
c11_11_11 2008-06-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 popule_daisy 的回复:]
给一个css吧,那样实用一点,

HTML code
<style>
#td { display: block; width: 440px; overflow: hidden; white-space: nowrap; -o-text-overflow: ellipsis; text-overflow: ellipsis; }
</style>




超过440px,td里面的内容就会用...代替


祝楼主好运!

接分啦,o(∩_∩)o...哈哈
[/Quote]
大自然D使者 2008-06-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wangjun8868 的回复:]
C# code
public static string PLeft(object Content, int length)
{
if ((Content == null) || !(Content is string))
{
return string.Empty;
}
if (((string)Content).Length > length)
{
Content = ((string)Content).Substring(0, length - 2) + "...";
}
return (string)Content;
}





HTML code
aspx:

[/Quote]
推荐用这种方式。
viaivi 2008-06-25
  • 打赏
  • 举报
回复

/// <summary>
/// 字符串截取函数
/// </summary>
/// <param name="str">要截取的字符串</param>
/// <param name="num">截取后的长度</param>
/// <returns></returns>
public static string subString(string str, int num)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
str.Trim();

if (str.Length < num)
return str;
else
return str.Substring(0, num);
}

<%# subString(Eval("chanpinshuoming"), 56))%>

bj890 2008-06-25
  • 打赏
  • 举报
回复
什么 ?
bj890 2008-06-25
  • 打赏
  • 举报
回复
感谢大家问题已经解决了!
前台html:
<asp:GridView ID="GridView1" runat="server" Style="position: relative; left: 47px; top: 1px;" AutoGenerateColumns="False" Width="343px" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="姓名">
<ItemTemplate>
<%#SubStr(Eval("name").ToString(),2)%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

后台.cs:
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "..";
return sNewStr;
}
leaohong 2008-06-24
  • 打赏
  • 举报
回复
推荐CSS
popule_daisy 2008-06-24
  • 打赏
  • 举报
回复
给一个css吧,那样实用一点,

<style>
#td { display: block; width: 440px; overflow: hidden; white-space: nowrap; -o-text-overflow: ellipsis; text-overflow: ellipsis; }
</style>


超过440px,td里面的内容就会用...代替


祝楼主好运!

接分啦,o(∩_∩)o...哈哈
编程有钱人了 2008-06-24
  • 打赏
  • 举报
回复

public static string PLeft(object Content, int length)
{
if ((Content == null) || !(Content is string))
{
return string.Empty;
}
if (((string)Content).Length > length)
{
Content = ((string)Content).Substring(0, length - 2) + "...";
}
return (string)Content;
}



aspx:
<%# PRight(PLeft(Eval("chanpinshuoming"), 56))%>
cl9876 2008-06-24
  • 打赏
  • 举报
回复
把数据库中的数据取到本地进行处理,例如dataset、datatable,让gridview的datasouce=dataset或datataable就可以了-------这是最笨的办法
GT7466 2008-06-24
  • 打赏
  • 举报
回复
截断超过长度的字符,再加上"..."

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧