后台的判断字段长短的代码是:
- C# code
public string SubStr(string sString, int nLeng) { if (sString.Length <= nLeng) { return sString; } string sNewStr = sString.Substring(0, nLeng); sNewStr = sNewStr + "..."; return sNewStr; }
现在不懂的是前台怎么写显示省略里面的信息啊
------解决方案--------------------------------------------------------
完整的宝贝粘贴可以测试的代码
- HTML code
<%@ 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>
------解决方案--------------------------------------------------------
11楼写的有问题啊,运行不出结果,有没有更详细点的答案