关闭

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

标签: ajaxbuttontextboxobjectsecuritystring
1434人阅读 评论(3) 收藏 举报
分类:

 

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();
        }
    }
}

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:162464次
    • 积分:2829
    • 等级:
    • 排名:第12643名
    • 原创:118篇
    • 转载:4篇
    • 译文:1篇
    • 评论:21条
    文章分类
    最新评论