ajax实现评论

 

Ajax做的评论,说起ajax打架都应该知道吧,除了不是这个行业的人们。Ajax最大的特点就是没有刷新,不影响数据的加载。给用户良好的体验。下面就是我用ajax做的一个仿网页,的新闻评论的一个小功能可以试着添加以下这个功能。

<divid="div1">

            已有<asp:Label ID="Label1"runat="server"></asp:Label>

            <asp:TextBoxID="TextBox1" runat="server" Height="50px"MaxLength="600" Rows="6"

                TextMode="MultiLine"Width="396px"></asp:TextBox>

        </div>

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

    </asp:ScriptManager>

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

        <ContentTemplate>

      

          

            <div style="float:right;">

            </div>

            <div style="width:390px">

                <asp:ButtonID="btnsay" runat="server" OnClick="btnsay_Click"Text="评论"

                    Width="45px"style="float:right;" />

            </div>

            <div id="div2">

                <asp:TextBoxID="TextBox2" runat="server" Rows="10"TextMode="MultiLine" Width="394px"

                   Height="256px"></asp:TextBox>

                <asp:ButtonID="btnfirst" runat="server"OnClick="btnfirst_Click" Text="首页" />

                &nbsp;<asp:ButtonID="btnadd" runat="server" OnClick="btnadd_Click"Text="上一页" />

                &nbsp;<asp:ButtonID="btnnext" runat="server"OnClick="btnnext_Click" Text="下一页" />

                &nbsp;<asp:ButtonID="btnlast" runat="server"OnClick="btnlast_Click" Text="尾页" />

                &nbsp;<inputid="btnspeak" type="button" value="立即评论" οnclick="aa()" /></div>

        </ContentTemplate>

</asp:UpdatePanel>

下面是后台代码:

publicpartial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender,EventArgs e)

        {

            geshu();

            if (!IsPostBack)

            {

                ViewState["@pageindex"]= "2";

                fennei();

            }

        }

 

        private void fennei()

        {

            this.TextBox2.Text = " ";

            string str =ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;

            using (SqlConnection sqlCn = newSqlConnection(str))

            {

                SqlCommand sqlCm =sqlCn.CreateCommand();

                sqlCm.CommandText ="fenye";

                sqlCm.CommandType =CommandType.StoredProcedure;

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

               sqlCm.Parameters.AddWithValue("@pagesize", 5);

                sqlCn.Open();

                SqlDataReader reader =sqlCm.ExecuteReader();

                if (reader.HasRows)

                {

                    while (reader.Read())

                    {

                        TextBox2.Text +="\n" + Convert.ToString(reader["saytime"]) + "\n"+ Convert.ToString(reader["content"]) + "\n" +Convert.ToString(reader["username"]);

                    }

                }

                sqlCm.Dispose();

                sqlCm.Dispose();

            }

        }

 

        private void geshu()

        {

            string str =ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;

            using (SqlConnection sqlCn = newSqlConnection(str))

            {

                SqlCommand sqlCm =sqlCn.CreateCommand();

                sqlCm.CommandText ="select count(*) from PlContent";

                sqlCn.Open();

                object obj =sqlCm.ExecuteScalar();

                this.Label1.Text = obj + "参与了评论";

            }

        }

 

        protected void btnsay_Click(objectsender, EventArgs e)

        {

            string str =Request["username"];

            string times = DateTime.Now.ToString();

            if (str != null)

            {

                string strs =ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;

                using (SqlConnection sqlCnn =new SqlConnection(strs))

               {

                    SqlCommand sqlCmm =sqlCnn.CreateCommand();

                    sqlCmm.CommandText ="insert intoPlContent(username,content,saytime)values(@username,@content,@datetime)";

                    sqlCmm.Parameters.AddWithValue("@username",str);

                   sqlCmm.Parameters.AddWithValue("@content", TextBox1.Text);

                   sqlCmm.Parameters.AddWithValue("@datetime", times);

                    sqlCnn.Open();

                    sqlCmm.ExecuteNonQuery();

                    sqlCmm.Dispose();

                    sqlCnn.Dispose();

                    fennei();

                    //string speak =TextBox1.Text;

                    //TextBox2.Text +="\n" + times + "\n" + speak + "\n" + str;

                }

            }

            else

            {

                Response.Write("请登录");

            }

        }

 

        protected void btnfirst_Click(objectsender, EventArgs e)

        {

            ViewState["@pageindex"] ="1";

            fennei();

        }

 

        protected void btnadd_Click(objectsender, EventArgs e)

        {

            int page =Convert.ToInt32(ViewState["@pageindex"]);

            if (page > 1)

            {

                page--;

            }

            ViewState["@pageindex"] =page;

            fennei();

        }

 

        protected void btnnext_Click(objectsender, EventArgs e)

        {

            int page =Convert.ToInt32(ViewState["@pageindex"]);

            quanbu();

            if (page <Convert.ToInt32(ViewState["@pageindex"]))

            {

                page++;

            }

            ViewState["@pageindex"] =page;

            fennei();

        }

 

        protected void btnlast_Click(objectsender, EventArgs e)

        {

            quanbu();

            fennei();

        }

 

        private int quanbu()

        {

            string str =ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;

            using (SqlConnection sqlCn = newSqlConnection(str))

            {

                SqlCommand sqlCm =sqlCn.CreateCommand();

                sqlCm.CommandText ="select count(*) from PlContent";

                sqlCn.Open();

                object obj =sqlCm.ExecuteScalar();

                if (obj != null)

                {

                    int i;

                    if (Convert.ToInt32(obj) %5 == 0)

                    {

                        i =Convert.ToInt32(obj) / 5;

                       ViewState["@pageindex"] = i;

                        returnConvert.ToInt32(ViewState["@pageindex"]);

                    }

                    else

                    {

                        i =Convert.ToInt32(obj) / 5 + 1;

                       ViewState["@pageindex"] = i;

                        returnConvert.ToInt32(ViewState["@pageindex"]);

                    }

                }

            }

            return 1;

        }

}

这是我做的一个小的评论可以跟数据库进行交互的,但是从页面看起来没有刷新的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值