后台的判断字段长短的代码是:
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}现在不懂的是前台怎么写显示省略里面的信息啊
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}现在不懂的是前台怎么写显示省略里面的信息啊
控件的ToolTip属性里面放没有截取过的字符串Text里放你调用过SubStr方法后返回的字符串
如果要好的效果,可以用一些Jquery ToolTips插件http://www.open-open.com/ajax/Tooltips.htm
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%#SubStr(Eval("choice_title").ToString(),5) %>' ToolTip='<%#Eval("choice_title") %>'></asp:Label></ItemTemplate>
<ItemStyle Wrap="False" />
$(document).ready(function() {
$("#ID").mouseover(function() {
$(this).text($(this).attr("title"));
});
$("#ID").mouseout(function() {
vat str=$(this).attr("title");
if ( str.length> 10)
$(this).text(str.substring(0,10)+"...");
else
$(this).text($(this).attr("title"));
});
});
把完整内容附在类似title上,鼠标移上去就显示全部内容,鼠标离开就显示前10个字符+...
还有 在后台可以添加鼠标移上去的事件么。
<%@ Page Language="C#" EnableViewState="true" %><%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public System.Data.DataTable CreateDataTable()
{
System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
System.Data.DataRow dr;
dataTable1.Columns.Add(new System.Data.DataColumn("RZId", typeof(System.Int32)));
dataTable1.Columns.Add(new System.Data.DataColumn("choice_title", typeof(System.String)));
dataTable1.PrimaryKey = new System.Data.DataColumn[] { dataTable1.Columns["RZId"] }; for (int i = 0; i < 8; i++)
{
dr = dataTable1.NewRow();
dr[0] = i;
dr[1] = "【孟子E章孟子E章孟子E章孟子E章孟子E章孟子E章孟子E章孟子E章孟子E章孟子E章孟子E章】" + i.ToString();
dataTable1.Rows.Add(dr);
}
return dataTable1;
} protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = CreateDataTable();
GridView1.DataBind();
}
} public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body onclick="myfunction()">
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%#SubStr(Eval("choice_title").ToString(),5) %>'
ToolTip='<%#Eval("choice_title") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
e.Row.Cells[itake].Text = "<a href=\"#\" style=\"color:#0000ff\" title=\"" + e.Row.Cells[itake].Text + "\" onclick=\"ShowTA('" + e.Row.Cells[0].Text + "')\">" + e.Row.Cells[itake].Text.Substring(0, 16) + "...</a>";
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ e.Row.Cells[下标从0开始].Text=e.Row.Cells[下标从0开始].Text.Substring(0, 16) + "...";
e.Row.Cells[下标从0开始].ToolTip = e.Row.Cells[下标从0开始].Text; }
}