您的位置:首页 > 编程语言

Ajax无刷新的微博系统代码(后台)

2012-03-22 20:03 162 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

public partial class Homework : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

ViewState["pageindex"] = 1;

ViewState["pagenums"] = 0;

DatabindGrid();





}



CalcRecords();

CreateDh();

}



protected void Button1_Click(object sender, EventArgs e)

{

//用来存放页数

if (Session["user"] != null)

{

Pl();

}

else

{

ScriptManager.RegisterStartupScript(this, GetType(), "as", "<script>alert('请您先登录!')</script>", false);

}

}

private void Pl()

{

string strcon = "server=.\\sqlexpress;initial catalog=MyTest;integrated security=true";

using (SqlConnection con = new SqlConnection(strcon))

{

using (SqlCommand cmd = new SqlCommand("insert Mytext values(@name,@msg,@from,@date)", con))

{

con.Open();

cmd.Parameters.AddWithValue("@name", Session["user"].ToString());

cmd.Parameters.AddWithValue("@msg", TextBox1.Text.Trim());

cmd.Parameters.AddWithValue("@from", "河北软件");

cmd.Parameters.AddWithValue("@date", System.DateTime.Now.ToString());

cmd.ExecuteNonQuery();

ScriptManager.RegisterStartupScript(this, GetType(), "as", "<script>alert('评论成功!')</script>", false);

DatabindGrid();

CalcRecords();

}

}

}

int pagenum = 10;

protected void DatabindGrid()

{

string strcon = "server=.\\sqlexpress;initial catalog=MyTest;integrated security=true";

using (SqlConnection con = new SqlConnection(strcon))

{

using (SqlCommand cmd = new SqlCommand("pageyouku",con))

{

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@pageindex",ViewState["pageindex"]);

cmd.Parameters.AddWithValue("@pagenum",pagenum.ToString());

DataTable dt = new DataTable();

SqlDataAdapter sda = new SqlDataAdapter(cmd);

sda.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();

}

}

if (Convert.ToInt32(ViewState["pageindex"].ToString()) == Convert.ToInt32(ViewState["pagenums"].ToString()))

{

// ScriptManager.RegisterStartupScript(this, GetType(), "as", "<script>alert('评论成功!')</script>", false);

Label1.Text = "第" + ((Convert.ToInt32(ViewState["pageindex"].ToString()) - 1)*10).ToString() + "-" + Label3.Text.Trim();

}

else

{

Label1.Text = "第" + (Convert.ToInt32(ViewState["pageindex"].ToString()) * 10 - 9).ToString() + "-" + (Convert.ToInt32(ViewState["pageindex"].ToString()) * 10).ToString();

//Label1.Text ="第"+

}

}

//创建导航条

protected void CalcRecords()

{

// int pagenums = 0;

string strcon = "server=.\\sqlexpress;initial catalog=MyTest;integrated security=true";

using (SqlConnection con = new SqlConnection(strcon))

{

using (SqlCommand cmd = new SqlCommand("select count(*) from Mytext", con))

{

con.Open();

object obj = cmd.ExecuteScalar();

if (obj!=null)

{

Label3.Text = obj.ToString();

if (Convert.ToInt32(obj.ToString()) % 10 == 0)

{

ViewState["pagenums"] = Convert.ToInt32(obj.ToString()) / 10;

}

else

{

ViewState["pagenums"] = Convert.ToInt32(obj.ToString()) / 10 + 1;

}

}

}

}







}

//单击分页

void btn_Command(object sender, CommandEventArgs e)

{

//throw new NotImplementedException();

// ScriptManager.RegisterStartupScript(this, GetType(), "as", "<script>alert('评论成功!')</script>", false);

Button btn = (Button)sender;

ViewState["pageindex"] = btn.Text.Trim();

DatabindGrid();

CalcRecords();

}

protected void CreateDh()

{

for (int i = 1; i <= Convert.ToInt32(ViewState["pagenums"].ToString()); i++)

{

Button btn = new Button();

btn.Text = i.ToString();

btn.Command += new CommandEventHandler(btn_Command);

if (!btn.Controls.Contains(btn))

{

dh.Controls.Add(btn);

}

}

}

protected void Button4_Click(object sender, EventArgs e)

{

string strcon = "server=.\\sqlexpress;initial catalog=MyTest;integrated security=true";

using (SqlConnection con = new SqlConnection(strcon))

{

using (SqlCommand cmd = new SqlCommand("select count(*) from T_user wherename=@name and
password=@pwd", con))

{

cmd.Parameters.AddWithValue("@name", TextBox2.Text.Trim());

cmd.Parameters.AddWithValue("@pwd", TextBox3.Text.Trim());

con.Open();

object obj = cmd.ExecuteScalar();

if (obj != null)

{

if (Convert.ToInt32(obj) == 1)

{

Session["user"] = TextBox2.Text.Trim();

}

else

{

ClientScript.RegisterClientScriptBlock(GetType(), "aa", "<script>alert('密码或用户名在误!')</script>");

}

}

}

}

}

protected void Button5_Click(object sender, EventArgs e)

{

if (Convert.ToInt32(ViewState["pageindex"].ToString())>1)

{

ViewState["pageindex"]=Convert.ToInt32(ViewState["pageindex"].ToString())-1;

DatabindGrid();

}

}

protected void Button6_Click(object sender, EventArgs e)

{

if (Convert.ToInt32(ViewState["pageindex"].ToString()) < Convert.ToInt32(ViewState["pagenums"].ToString()))

{

ViewState["pageindex"] = Convert.ToInt32(ViewState["pageindex"].ToString()) +1;

DatabindGrid();

}

}

}





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: