返回首页
当前位置: 主页 > 网络编程 > Asp实例教程 >

实现新闻信息详细显示页面(利用类的方法实现)程序代码

时间:2010-06-02 21:07来源:知行网www.zhixing123.cn 编辑:麦田守望者

一、新闻页面显示利用不同控件的实现读取功能

1.利用Repeater控件实现的并加上调用类的方法

 

 

.aspx文件

<asp:Repeater ID="Repeater1" runat="server">

<HeaderTemplate>

<a href="more.aspx?BigClassId=1" target=_blank>新闻资讯</a><div class="Top_line"></div>

</HeaderTemplate>//显示标题模板

<ItemTemplate>

<div class="list_x">

<img src="images/Soft_common.gif" /> <a href="News_show.aspx?ID=<%# Eval("id")%>" target=_blank><%# customdata.CutString(Eval("title").ToString(), 60)%></a>  <%# customdata.Showdata((DateTime)Eval("Addtime"), "m-d")%><br />

</div>

</ItemTemplate>

</asp:Repeater>

.aspx.cs文件

1.1实现功能代码的后台代码

protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            bind();//读取

        }

首先定义void方法

这里对代码举例说明

调用customdata类的GreatDs()方法 执行相应查询语句,根据你定义控件的ID显示不同信息

例如:

string mysql = "SELECT top 8 BigClassID,ID,Title,AddTime FROM News  where BigClassID=1 order by addtime desc";

Repeater1.DataSource = customdata.GreatDs(mysql);

Repeater1.DataBind();

这里的Repeater1就是你在前台定义的ID名称

首页各栏目信息显示处理如下:

void bind()

    {

        //读取新闻资讯

        string mysql = "SELECT top 8 BigClassID,ID,Title,AddTime FROM News  where BigClassID=1 order by addtime desc";

        Repeater1.DataSource = customdata.GreatDs(mysql);

        Repeater1.DataBind();

        //网页设计

        string mysql1 = "SELECT top 8 class.ClassName,news.BigClassID,ID,Title,AddTime FROM News,class  where class.classid=news.classid and news.BigClassID=2 order by addtime desc";

        DataListweb.DataSource = customdata.GreatDs(mysql1);

        DataListweb.DataBind();

        //asp

        string mysql2 = "SELECT top 8 ID,Title,AddTime FROM News where classid=12 order by addtime desc";

        DataListasp.DataSource = customdata.GreatDs(mysql2);

        DataListasp.DataBind();

        //php

        string mysql3 = "SELECT top 8 ID,Title,AddTime FROM News where classid=13 order by addtime desc";

        DataListphp.DataSource = customdata.GreatDs(mysql3);

        DataListphp.DataBind();

        //asp.net

        string mysql4 = "SELECT top 8 ID,Title,AddTime FROM News where classid=14 order by addtime desc";

        DataListaspnet.DataSource = customdata.GreatDs(mysql4);

        DataListaspnet.DataBind();

        //数据库

        string mysql5 = "SELECT top 8 class.classname,news.BigClassID,ID,Title,AddTime FROM News,class  where class.classid=news.classid and news.BigClassID=4 order by addtime desc";

        DataListadba.DataSource = customdata.GreatDs(mysql5);

        DataListadba.DataBind();

        //服务器

        string mysql6 = "SELECT top 8 class.classname,news.BigClassID,ID,Title,AddTime FROM News,class  where class.classid=news.classid and news.BigClassID=5 order by addtime desc";

        DataListserver.DataSource = customdata.GreatDs(mysql6);

        DataListserver.DataBind();

        //网站运营

        string mysql7 = "SELECT top 8 class.classname,news.BigClassID,ID,Title,AddTime FROM News,class  where class.classid=news.classid and news.BigClassID=6 order by addtime desc";

        DataListyy.DataSource = customdata.GreatDs(mysql7);

        DataListyy.DataBind();

        //搜索优化

        string mysql8 = "SELECT top 8 class.classname,news.BigClassID,ID,Title,AddTime FROM News,class  where class.classid=news.classid and news.BigClassID=8 order by addtime desc";

        DataListyh.DataSource = customdata.GreatDs(mysql8);

        DataListyh.DataBind();

    }

customdata.cs文件(自定义类文件,存放在App_Code文件夹中)

using System.Data.SqlClient;

    //连库字符串

    public static string connstring = Convert.ToString(ConfigurationManager.ConnectionStrings["MySqlConnection"]);

    public static DataSet GreatDs(string sql)

    {//执行查询处理

        SqlDataAdapter Da = new SqlDataAdapter(sql, connstring);

        DataSet ds = new DataSet();

        Da.Fill(ds);

        return ds;

    }

    public static void DoSql(string sql)

    {//执行插入、更新、删除

        SqlConnection conn = new SqlConnection(connstring);//创建连接对象

        conn.Open();//打开数据库

        SqlCommand cmd = new SqlCommand(sql, conn);

        cmd.ExecuteNonQuery();//

        conn.Close();//关闭数据库

    }

    //日期输出格式

    public static string Showdata(DateTime inputdate,string strformat)

    {

        string outstr="";

        switch(strformat)

        {

            case "m-d":

                outstr=" [<font color=red>"+inputdate.Month.ToString()+"-"+inputdate.Day.ToString()+"</font>]";

                break;

            case "y-m-d":

                outstr=" ["+inputdate.ToShortDateString()+"]";

                break;

        }

        return outstr;

    }

    /// <summary>

    /// 截取字符串

    /// </summary>

    /// <param name="strInput">输入字符串</param>

    /// <param name="intLen"></param>

    /// <returns></returns>

    public static string CutString(string strInput, int intLen)

    {

        strInput = strInput.Trim();

        byte[] buffer1 = Encoding.Default.GetBytes(strInput);

        if (buffer1.Length > intLen)

        {

            string text1 = "";

            for (int num1 = 0; num1 < strInput.Length; num1++)

            {

                byte[] buffer2 = Encoding.Default.GetBytes(text1);

                if (buffer2.Length >= (intLen - 4))

                {

                    break;

                }

                text1 = text1 + strInput.Substring(num1, 1);

            }

            return (text1 + "...");

        }

        return strInput;

}

2.1.利用DataList控件实现的并加上调用类的方法

 

实现代码在上面的代码中

注意: 在对控件定义ID名字的时候一定要不同并且利于记忆。

二、新闻详细页面功能实现剖析

显示使用Repeater控件实现,调用类的方法执行查询语句 ,效果如下图所示及界面代码如下:

效果图示:

 

.aspx文件:

        <asp:Repeater ID="shownew" runat="server">

        <ItemTemplate>

            <table width="90%" border="0">

              <tr>

                <td>&nbsp;<%# Eval("Title")%></td>

              </tr>

              <tr>

                <td>&nbsp;<%# Eval("ReadCishu")%></td>

              </tr>

              <tr>

                <td align="left">&nbsp;<%# Eval("Content") %></td>

              </tr>

              <tr>

                <td><%# Eval("Author")%>&nbsp;<%# customdata.Showdata((DateTime)Eval("Addtime"), "y-m-d")%></td>

              </tr>

            </table>

       

        </ItemTemplate>

        </asp:Repeater>

(一)详细显示新闻信息页面,根据据新闻标题带过来的ID号读取信息

 代码:

在加载页面时进行新闻信息查询、显示

protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack) 

        {

            if (Convert.ToInt32(Request.QueryString["id"]) != null)

            {

                string sql = "select * from news where id=" + Convert.ToInt32(Request.QueryString["id"]);

                shownew.DataSource = customdata.GreatDs(sql);

                shownew.DataBind();

            }

        }

    }

注意:切记在取ID的时候一定要将ID转换为整型

 Convert.ToInt32(Request.QueryString["id"])

 

(二) 新闻浏览次数要要在客户每浏览一次,浏览次数就增加一次,实现的原理就是对“浏览次数”字段进行加1操作。

定义一个方法UpdateRead,调用customdata类的DoSql方法执行语update语句

    public void UpdateRead()

{//更新阅读次数

string sql1 = "update news set ReadCishu=ReadCishu+1 where id=" + Convert.ToInt32(Request.QueryString["id"]);

    customdata.DoSql(sql1);

}

在加载页面时调用UpdateRead()方法如下:

protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack) 

        {

            if (Convert.ToInt32(Request.QueryString["id"]) != null)

            {

                string sql = "select * from news where id=" + Convert.ToInt32(Request.QueryString["id"]);

                shownew.DataSource = customdata.GreatDs(sql);

                shownew.DataBind();

            }

        }

UpdateRead();//调用更新浏览次数方法

    }

------分隔线----------------------------
标签(Tag):ASP.NET教程 ASP.NET代码 ASP.NET实例
------分隔线----------------------------
推荐内容
猜你感兴趣