关于GridView中超长字符串显示时,我们想如此处理:截取其中前几个字符串,然后在这几个字符串后面加"...",而且通过某种方式能查看超长字符串的内容?
解决方案 »
- Eval 引号问题 在线等
- 所有分都在这里了!ActiveX控件如何与页面互访 asp.net
- 数据绑定的问题
- server03 iis6 下发布.NET4.0站点无法显示该页
- 想问下我的网站在个人的机器上用IIS浏览没问题,怎么上传到购买的空间里就会有问题,而且无法访问!
- 关于网络相册
- 问个关于提交的问题
- 如何触发datalist的itemcommand事件
- 为什么不能update,大侠请帮手。
- 有关数组的问题,不解,求助,在线
- ASP.net如何做界面设计
- 据说C#是在参考JAVA等语言优缺点基础上发展而来的,其先进特性一大箩筐,但没见着几个大型项目应用典型,这CSDN又慢,还老调整老折腾......
或者放到你cell的alt里面,鼠标放上去停顿下就会出现,不过估计浏览者不会有那个耐心。
还有就是别人点击后在另外的窗口显示内容。
再有就是利用ajax技术,别人点击你显示的内容,你用ajax到后台获取全部内容用div包括显示出来。
//解决方法:数据绑定后过滤每一行即可
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView mydrv;
string gIntro;
if (GridView1.PageIndex == 0)
{
mydrv = myds.Tables["表名"].DefaultView[i];//表名
gIntro = Convert.ToString(mydrv["字段"]);//所要处理的字段
GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
}
else
{
mydrv = myds.Tables["表名"].DefaultView[i + (5 * GridView1.PageIndex)];
gIntro = Convert.ToString(mydrv["字段"]);
GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
}
} //调用的方法: public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
//希望能给你帮助
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.content").ToString().Length>10?DataBinder.Eval(Container, "DataItem.content").SubString(0,10)+"...":DataBinder.Eval(Container, "DataItem.content").ToString() %>' ToolTip='<%# DataBinder.Eval(Container, "DataItem.content").ToString()%>'>
</asp:Label>
</ItemTemplate>
{
//控制字符显示长度,若大于18个字符,显示前18个,后加"..."
for (int i = 0; i <e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text.Length > 18)
{
e.Row.Cells[i].Text = e.Row.Cells[i].Text.Substring(0, 18) + "...";
}
} }
/// <summary>
/// 裁取指定长度的字符串,被裁取的字符串以"..."替代。
/// </summary>
/// <param name="mString">用于要裁取的字符串</param>
/// <param name="mLength">长度</param>
/// <returns>返回String</returns>
public string SubString(string mString, int mLength)
{
if (mString.Length > mLength)
return mString.Substring(0, mLength-2) + "...";
else
return mString;
}
然后在GridView中用模板的方法绑定
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="超长字符">
<ItemTemplate>
<%# SubString(Eval("超长字符字段名").ToString(), 18) %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<ItemTemplate>
<asp:Label ID="Lable1" runat="server" Text='<%#Eval("家庭住址").ToString().Length > 10 ? Eval("家庭住址").ToString().Substring(0, 10) + "..." : Eval("家庭住址")%> ' ToolTip='<%# Eval("家庭住址").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Lable1" runat="server" Text=' <%#Eval("家庭住址").ToString().Length > 10 ? Eval("家庭住址").ToString().Substring(0, 10) + "..." : Eval("家庭住址")%> ' ToolTip=' <%# Eval("家庭住址").ToString()%>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
最近做的一个项目就是这样用的,不错(⊙o⊙)哦!
case
when len(pDescription)>3
then left(pDescription,3)+'...'
else pDescription
end
as Description
from Products
<ItemTemplate>
<asp:Label ID="Lable1" runat="server" Text=' <%#Eval("家庭住址").ToString().Length > 10 ? Eval("家庭住址").ToString().Substring(0, 10) + "..." : Eval("家庭住址")%> ' ToolTip=' <%# Eval("家庭住址").ToString()%>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
学习
GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
就可以了.试试吧!
{ string CellText = e.Row.Cells[cellIndex].Text.Trim(); if (CellText.Length > maxStrLength)
{ e.Row.Cells[cellIndex].Text = CellText.Substring(0, maxStrLength - 1) + "<strong style='Color:blue'> ...</strong>"; e.Row.Cells[cellIndex].ToolTip = CellText;
} }
<Columns>
<asp:TemplateField HeaderText="超长字符">
<ItemTemplate>
<%# SubString(Eval("超长字符字段名").ToString(), 18) %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
可行
<%#(DataBinder.Eval(Container.DataItem, "new_content ")).ToString().Length < 115 ? (DataBinder.Eval(Container.DataItem, "new_content ")) : ((DataBinder.Eval(Container.DataItem, "new_content ")).ToString().Substring(0, 115)+"......")%>
{
if(InputStr.length<=nleng)
{
return InputStr;
}
else
{
string NewStr=InputStr.SubString(0,nleng);
NewStr=NewStr+"......";
return NewStr;
}
}
<%#SubStr(DataBinder.Eval(Container.DataItem,"Content").ToString()),5%>
二种方法:
GridView_RowDataBound()
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
if(e.Row.Cells[2].Text.Length>=5)
{
e.Row.Cells[2].Text=e.Row.Cells[2].Text.SubString(0,5)+"..........";
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string strName = e.Row.Cells[0].Text;
if (strName.Length > 5)
e.Row.Cells[0].Text = strName.Substring(0, 5) + "....";
e.Row.Cells[0].ToolTip = strName;
}
}