前往顾页
以后地位: 主页 > 收集编程 > 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名字的时候必然要不归并且利于记忆。

2、消息详细页面服从实现分解

显现利用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();//调用更新浏览次数体例

    }

顶一下
(1)
100%
踩一下
(0)
0%
------分开线----------------------------
标签(Tag):ASP.NET教程 ASP.NET代码 ASP.NET实例
------分开线----------------------------
颁发评论
请自发遵循互联网相关的政策法规,严禁公布色情、暴力、革命的谈吐。
评价:
神色:
考证码:点击我更换图片
猜你感兴趣