有缺陷的留言板

来源:互联网 发布:林仕鼎 知乎 编辑:程序博客网 时间:2024/04/30 11:31

 

 

页面设置:

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="¢??ã?.aspx.cs"Inherits="ajax.¢??ã?"%>

 

<%@Registerassembly="CKEditor.NET"namespace="CKEditor.NET"tagprefix="CKEditor"%>

 

<!DOCTYPEhtml PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

    <title></title>

    <styletype="text/css">

        body

    {

        width:960px;

        height:900px;

        position:relative;

        margin:0pxauto;

            top:0px;

            left:0px;

        }

    #content

    {

        width:960px;

        height:900px;

    }

    #liuyan

    {

        width:960px;

        height:281px;

        float:left;

    

    }

   #contents

   {

       width:960px;

       height:558px;

       float:left;

     

      

   }

    </style>

  <%-- ScriptManager.RegisterStartupScript(this,this.GetType(), "","<script>openLayer('test_con3')</script>", false);--%>

 

</head>

<body>

    <formid="form1" runat="server">

    <asp:ScriptManagerID="ScriptManager1"runat="server">

    </asp:ScriptManager>

    <asp:UpdatePanelID="UpdatePanel1" runat="server">

    <ContentTemplate>

     <divid="content">

    <divid="liuyan">

        <CKEditor:CKEditorControlID="cke" runat="server" Height="202px"></CKEditor:CKEditorControl>

        <br/>

 

        <asp:ButtonID="btnpl" runat="server" Text="¨¤?"onclick="btnpl_Click"/>

    

 

        &nbsp;&nbsp;

        <asp:ButtonID="Button1" runat="server" onclick="Button1_Click"Text="Ì??"/>

        </div>

    <divid="contents">

        <asp:GridViewID="GridView1" runat="server" AutoGenerateColumns="False"

            style="text-align: left;margin-top: 100px;"Width="100%" Height="100%">

            <Columns>

                <asp:BoundFieldDataField="name" HeaderText="®?¨ª"/>

                <asp:BoundFieldDataField="contents"HeaderText="¨²¨Y"/>

                <asp:BoundFieldDataField="time" HeaderText="¢??º¡À?"/>

            </Columns>

        </asp:GridView>

        </div>

        <asp:ButtonID="btnno1" runat="server" Text="º¡Á°3"onclick="btnno1_Click"/>

        <asp:ButtonID="btnlast" runat="server" Text="¦?°?°3"onclick="btnlast_Click"/>

        <asp:ButtonID="btnnext" runat="server" Text="?°?°3"onclick="btnnext_Click"/>

        <asp:ButtonID="btnmoye" runat="server" Text="?°3"onclick="btnmoye_Click"/>

    </div>

    </ContentTemplate>

    </asp:UpdatePanel>

  

    </form>

</body>

</html>

 

后台代码:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

 

namespace ajax

{

    public partial class ¢??ã? : System.Web.UI.Page

    {

        intpagesize = 4;

        protectedvoid Page_Load(objectsender,EventArgs e)

        {

 

            if(!IsPostBack)

            {

                ViewState["pageindex"] ="1";

                get();

            }

 

 

            if(Session["name"] ==null)

            {

                this.ClientScript.RegisterClientScriptBlock(this.GetType(),"OK","<script type='text/javascript'>alert('?¨²¨¨Ì??ê?¨²?D¢??ê?');</script>");

            }

            else

            {

                Button1.Visible = false;

            }

 

          

          

 

        }

 

        protectedvoid btnpl_Click(objectsender,EventArgs e)

        {

            stringstr = "data source=LENOVO-PC\\SQLEXPRESS;database=MyTest;IntegratedSecurity=true";

            SqlConnectionconn =new SqlConnection(str);

            SqlCommandcomm = new SqlCommand();

            comm.Connection = conn;

            comm.CommandText = "insert into liuyanban (name,contents,time) values(@name,@contents,@time)";

            SqlParameterparam =new SqlParameter("@name",Session["name"].ToString());

            comm.Parameters.Add(param);

            param =newSqlParameter("@contents",cke.Text);

            comm.Parameters.Add(param);

            param =newSqlParameter("@time",DateTime.Now);

            comm.Parameters.Add(param);

            conn.Open();

            comm.ExecuteNonQuery();

            this.GridView1.DataSource=null;

            get();

            comm.Dispose();

            conn.Dispose();

        }

 

        privatevoid get()

        {

            stringstr = "datasource=LENOVO-PC\\SQLEXPRESS;database=MyTest;Integrated Security=true";

            SqlConnectionconn =new SqlConnection(str);

 

            SqlCommandcomm = new SqlCommand();

            comm.Connection = conn;

            comm.CommandText = "fenye";

            comm.CommandType = CommandType.StoredProcedure;

            comm.Parameters.AddWithValue("@pageindex",Convert.ToInt32(ViewState["pageindex"]));

            comm.Parameters.AddWithValue("@pagesize", pagesize);

            SqlDataAdapteradpter =new SqlDataAdapter(comm);

            DataTabledt = new DataTable();

            adpter.Fill(dt);

            this.GridView1.DataSource= dt;

            this.GridView1.DataBind();

            comm.Dispose();

            conn.Dispose();

        }

 

        protectedvoid Button1_Click(objectsender,EventArgs e)

        {

            Response.Redirect("denglu.aspx");

        }

 

        protectedvoid btnno1_Click(objectsender,EventArgs e)

        {

            ViewState["pageindex"]="1";

            get();

        }

 

        protectedvoid btnlast_Click(objectsender,EventArgs e)

        {

            intpageindex = Convert.ToInt32(ViewState["pageindex"]);

            if(pageindex > 1)

            {

                pageindex--;

            }

            ViewState["pageindex"]= pageindex.ToString();

            get();

        }

 

        protectedvoid btnnext_Click(objectsender,EventArgs e)

        {

            intpageindex = Convert.ToInt32(ViewState["pageindex"]);

            if(pageindex < GetTotalSize())

            {

                pageindex++;

            }

            ViewState["pageindex"]= pageindex.ToString();

            get();

        }

 

        protectedvoid btnmoye_Click(objectsender,EventArgs e)

        {

            ViewState["pageindex"]= GetTotalSize();

            get();

        }

 

        privateint GetTotalSize()

        {

            stringstr = "datasource=LENOVO-PC\\SQLEXPRESS;database=MyTest;Integrated Security=true";

            SqlConnectionconn =new SqlConnection(str);

 

            SqlCommandcomm = new SqlCommand();

            comm.Connection = conn;

           conn.Open();

            comm.CommandText = "select count(*) from liuyanban";

            objectobj = comm.ExecuteScalar();

            if(obj != null)

            {

                inttotal = Convert.ToInt32(obj);

                if(total % pagesize == 0)

                {

                    returntotal / pagesize;

                }

                else

                {

                    returntotal / pagesize + 1;

                }

            }

            comm.Dispose();

            conn.Dispose();

            return0;

        }

    }

}

  存储过程:

 

USE [MyTest]

GO

/******Object:  StoredProcedure[dbo].[fenye]    Script Date: 03/22/201220:25:10 ******/

SET ANSI_NULLSON

GO

SET QUOTED_IDENTIFIERON

GO

--=============================================

-- Author:    <Author,,Name>

-- Createdate: <Create Date,,>

--Description:   <Description,,>

--=============================================

ALTER PROCEDURE[dbo].[fenye]

    -- Add the parameters for the storedprocedure here

    @pageindex int,

    @pagesize int

AS

BEGIN

    -- SET NOCOUNT ON added to preventextra result sets from

    -- interfering with SELECT statements.

    SET NOCOUNTON;

 

    -- Insertstatements for procedure here

    select name,contents,timefrom(selectROW_NUMBER() over(orderby id)asrownum,*fromliuyanban)t

where rownum>(@pageindex-1)*@pagesizeand rownum<=@pageindex*@pagesizeorder by id desc

END