标题过长解决办法

我直接将项目内容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("..........");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值