我直接將項目內容copy進來的,直接粘貼就ok了!~
前臺: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>新聞標題過長的解決辦法</title> </head> <body> <form id="form1" runat="server"> 第一種:代碼比較簡單,大家可以直接粘貼看效果!下面是用Css解決 經過測試如果有html標籤,它不會輸出 --------------------------------------------------------------------------- <div style="width:100px; overflow:hidden; text-overflow:ellipsis;">iii<span style="background-color: #0000ff">iiiiiiiiiiiiiiiiiiiiiiiii</span></div> <div style="width:100px;overflow: hidden;"><nobr>iii<span style="background-color: #0000ff">iiiiiiiiiiiiiiiiiiiiiiiii</span></nobr></div> <asp:DataList ID="dlstTitle" runat="server"> <ItemTemplate> <div style="width:100px; overflow:hidden; text-overflow:ellipsis;"> <lu><%# Eval("UserName") %></lu> </div> </ItemTemplate> </asp:DataList><br /> --------------------------------------------------------------------------------- 第二種:下面是用截取的方法,有個缺點,雖然他不會顯示html標籤,但是會將html的那部分用空格代替 --------------------------------------------------------------------------------- <asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <table style="background:blue;"> <tr><td style="width: 3px"><%# Eval("UserName").ToString().Length>4?Eval("UserName").ToString().Substring(0,4).ToString()+"...":Eval("UserName") %></td></tr> </table> </ItemTemplate> </asp:DataList> --------------------------------------------------------------------------------- 第三種:下面的方法是通過後臺截取完成的,優點就是在後臺將html標籤通過正則去掉再進行截取的 <asp:DataList ID="DataList2" runat="server"> <ItemTemplate> <table style="background:blue;"> <tr><td style="width: 3px"><a href="Default.aspx" _fcksavedurl=""Default.aspx"" title='<%# Eval("UserName") %>'><%# cut5(gs(Eval("UserName"))) %></a></td></tr> </table> </ItemTemplate> </asp:DataList> ---------------------------------------------------------------------------------
其實大家可以看到這三種方法css的比較好,而且也是最常用的! 但是不同方法有不同用途,所以大家看情況使用了 這是我自己的總結,也許什麼地方說錯了或者是實驗的和大家不同 希望大家能及時指正並告訴我,thank you ! </form> </body> </html>
後臺: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
using System.Data.SqlClient; using System.Text.RegularExpressions;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //數據綁定 string sql = "Select UserName From Test_User"; SqlConnection con = new SqlConnection(); con.ConnectionString = "Server=.;DataBase=Test;Uid=sa;Pwd="; SqlCommand cmd = new SqlCommand(sql, con); con.Open(); SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.Read()) { //DataList1.DataSource = sdr; //DataList1.DataBind(); //dlstTitle.DataSource = sdr; //dlstTitle.DataBind(); DataList2.DataSource = sdr; DataList2.DataBind();; } sdr.Close(); con.Close(); } }
//第三中方法的後臺截取------------------------------------------
public Object gs(Object aa) { object boby = aa;
object heihei = Server.HtmlDecode((string)(boby));
object tex = heihei; return tex; }
public string cut5(Object aa) { string cc = aa.ToString(); if (cc.Length >= 35) { //這裏是對字符串進行過濾再做截取工作 //這個表達式不夠應該不夠全面 //<(meta |link |/?o: |/?style |/?div |/?st/d |/?head |/?html |body |/?body |/?span |!/[)[^ >]*? > //這個大家可以換上,不行的話還可以自己再加標誌字符 cc = Regex.Replace(cc, @" <[^ >]* >", "", RegexOptions.IgnoreCase); cc = cc.Substring(0, 35) + "..."; return cc; } else { return "123456"; } }
//------------------------------------------------------------ //這樣彈出對話框可防止css在對話框結束後混亂的錯誤! //this.Page.RegisterClientScriptBlock("..........");
}