我做了个新闻系统。首页要求显示新闻标题,如果新闻标题大与20个字符 那么就显示20个字符 其他显示为省略号"...." 如果小与20个字符就全显示出来。
在首页点击了某条新闻后,数据库里该条新闻点击率加1,然后弹出新窗口显示新闻详细信息。要求就2点:
1.如何控制DataGrid和DataList 显示标题字符数量。
2.怎样才能把新闻点击加1 又弹出新窗口。原来我用 HyperLink控件 但是数据库点击率不知道怎么修改  又用LinkButton 不知道怎么弹出新页面。大侠们有什么好思路提醒下小弟

解决方案 »

  1.   

    第一个问题: 
       方法一,在数据库存储过程里用substring() 实现
       方法二:在页面数据绑定的时候实现如数据绑定时写个函数处理字符串
          <a href=".."><%# getString( DataBinder.Eval(Container.DataItem ,"xxxx"))%></a>
    第二个问题:
       我一般是在打开的页面里实现,在每个新闻页面打开之前马上在数据库里增加一次点击
      

  2.   

    1:
    if(标题.Length>20)
       标题="....";
    else
       标题= 标题;2:
    在弹出的窗口中对数据库进行操作,或在没有弹出窗口前使用无刷新技术对数据库操作
      

  3.   

    <%@ Page language="c#" Codebehind="2262537.aspx.cs" AutoEventWireup="false" Inherits="bsTest2005_8_16.CSDN._2262537" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>2262537</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">

    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
    AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn Visible="False" DataField="id"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="标题">
    <ItemTemplate>
    <asp:LinkButton id="LinkButton1" runat="server" CommandName="OpenTitle">
    <%# GetSubString(DataBinder.Eval(Container.DataItem, "title").ToString(),10)%>
    </asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="readTimes" HeaderText="阅读次数"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>
    </form>
    </body>
    </HTML>
    ************************************************
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace bsTest2005_8_16.CSDN
    {
    /// <summary>
    /// _2262537 的摘要说明。
    /// </summary>
        public class _2262537 : System.Web.UI.Page
        {
            protected System.Web.UI.WebControls.DataGrid DataGrid1;
        
            private void Page_Load(object sender, System.EventArgs e)
            {
                // 在此处放置用户代码以初始化页面
                if(!IsPostBack)
                    this.BindData();
            }        private void BindData()
            {
                //模拟出一些原始数据绑定DataGrid
                DataTable dt1 = new DataTable();
                dt1.Columns.Add("id");
                dt1.Columns.Add("title");
                dt1.Columns.Add("readTimes");            dt1.Rows.Add(new object[]{1,"erhaerhawega34 aw3ga w3a aw43ga3",0});
                dt1.Rows.Add(new object[]{2,"aba4h asdfa wf aw eaweg",1});
                dt1.Rows.Add(new object[]{3,"h阿比啊恶化sdaf噶额外牌花为",3});
                dt1.Rows.Add(new object[]{4,"wag23h32 adh",5});
                
                this.DataGrid1.DataSource=dt1;
                this.DataGrid1.DataBind();
            }        /// <summary>
            /// 按字节截断字符串。
            /// </summary>
            public static string GetSubString(string mText,int byteCount)
            {
                if(byteCount < 1 ) return mText;
         
                
                if(System.Text.Encoding.Default.GetByteCount(mText) <= byteCount)
                {
                    return mText;
                }
                else
                {
                    byte[] txtBytes = System.Text.Encoding.Default.GetBytes(mText);
                    byte[] newBytes = new byte[byteCount-6];
                    
                    for(int i=0;i<byteCount-6;i++)
                        newBytes[i] = txtBytes[i];                return System.Text.Encoding.Default.GetString(newBytes) + "...";
                }
            }
            private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
                if(e.CommandName == "OpenTitle")
                {
                    string contentID = e.Item.Cells[0].Text;
                    int readTime = Convert.ToInt32(e.Item.Cells[2].Text.Trim());
                    this.updateTitleReadTimes(contentID);
                    Response.Write("<script>window.open('webform1.aspx?id="+contentID+"');</script>");
                    e.Item.Cells[2].Text = (readTime+1).ToString();
                }
            }        /// <summary>
            /// 根据文章id到数据库更新阅读次数
            /// </summary>
            public  void updateTitleReadTimes(string id)
            {
                //string sql = "update table1 set readtimes = readtimes+1 where id = '"+id+"'";
            }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
                this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
                this.Load += new System.EventHandler(this.Page_Load);        }
    #endregion        
    }
    }
      

  4.   


    补充下  :
    后台代码protected string getString( object str)
    {
     string tt= str.ToString();
      if( tt.Length>20) tt=tt.SubString(0,20) + "...";
      return tt;
    }
      

  5.   

    然后,在新打开的窗口webform1.aspx里:
     private void Page_Load(object sender, System.EventArgs e)
     {
         string contentID = Request.QueryString["id"];
        //根据上面的id就可以从数据库里读取文章的详细内容并显示到当前页面了
     }
      

  6.   

    我原来用 模板列,里面用个LinkButton绑定要显示的新闻标题 然后在DataGrid的ItemDataBound属性里加入下面的代码:
    if (e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
    {
    string a=Convert.ToString(DataBinder.Eval(e.Item.DataItem,"Title"));if (a.Length >20) 
    {
    a=a.Substring(0,20)+"..";
    }
    e.Item.Cells[2].Text=a;
     }    以上方法实现了 字符数量的控制,但是    LinkButton的点击事件丢失了。
    不知道怎么解决
      

  7.   

    1.如何控制DataGrid和DataList 显示标题字符数量。
    在databind事件里判断一下就行了
    2.怎样才能把新闻点击加1 又弹出新窗口。
    打开新窗口先转到一个计数页在转回来显示新闻就的了呗
      

  8.   

    楼上说的让我有点思路,
    “ 在databind事件里判断一下就行了”具体怎么弄能不能把代码贴出来啊 
    还有个问题就是 中文和英文的区别。 中文和英文字节不同。 我想要如果是中文就显示5个字符出来。如果是英文就显示10个字符。。
      

  9.   

    页面:<DataGrid><a href='<%# getString( DataBinder.Eval(Container.DataItem ,"url"))%>'>
    <%# getString( DataBinder.Eval(Container.DataItem ,"title"))%>
    </a></DataGrid>后台代码
    protected string getString( object str)
    {
     string tt= str.ToString();
      if( tt.Length>20) tt=tt.SubString(0,20) + "...";
      return tt;
    }