GridView自定義列資料繫結,和自定義頒功能

c979170768發表於2011-11-15

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.Common;
using System.Configuration;
using System.Data.SqlClient;

public partial class Delete_hope : System.Web.UI.Page
{
    // string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    private int pagesize = 10;
    private int pagecount = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Server.Transfer("logo.aspx");
        }


        if (!Page.IsPostBack)
        {
            Init();
            DataLoad();
        }
    }
    private void Init()
    {

        int pagecon = Getcount();

        if (pagecon % pagesize == 0)
        {
            pagecount = pagecon / pagesize;
        }
        else
        {
            pagecount = pagecon / pagesize + 1;
        }
        ViewState["PageCount"] = pagecount;
        LinkButton5.Enabled = false;
    }
    private void DataLoad()
    {

        this.GridView1.DataSource = GetContents("", Convert.ToInt32(txtpage.Text), pagesize);
        this.GridView1.DataBind();
    }

    protected void btnfirst_Click(object sender, EventArgs e)
    {
        this.txtpage.Text = "1";
        LinkButton5.Enabled = false;
        DataLoad();
    }

    protected void btnlast_Click(object sender, EventArgs e)
    {
        //最後一頁
        LinkButton5.Enabled = true;
        this.txtpage.Text = ViewState["PageCount"].ToString();
        LinkButton6.Enabled = false;

        DataLoad();
    }

    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        //上頁
        int i = Convert.ToInt32(txtpage.Text);
        if (0 == (i-2))
        {
            LinkButton5.Enabled = false;
          
        }
        LinkButton6.Enabled = true;

        txtpage.Text = Convert.ToString(i - 1);
        DataLoad();
    }

    protected void LinkButton6_Click(object sender, EventArgs e)
    {
        //下頁
      
        int i = Convert.ToInt32(txtpage.Text);
        if (Convert.ToInt32(ViewState["PageCount"])-1 == i)
        {
            LinkButton6.Enabled = false;
         
        }
        LinkButton5.Enabled = true;
        txtpage.Text = Convert.ToString(i + 1);
        DataLoad();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        LinkButton5.Enabled = false;
        LinkButton6.Enabled = false;
        if (Convert.ToInt32(ViewState["PageCount"])>=Convert.ToInt32(txtpage.Text))
        DataLoad();

    }

    public int Getcount()
    {
        int i = 0;
        string s = "select count(*) from hopeperson";


        DataTable dt = ExecuSQLtable(s);
        i = Convert.ToInt32(dt.Rows[0][0]);
        return i;
    }
    string cmdTxt = "";
    public DataTable GetContents(string orderFields, int pagenum, int pagesize)
    {
        cmdTxt  = "Select Row_Number() Over (Order By ID) as RowNum,* From hopeperson  ";
        cmdTxt = "Select * From (" + cmdTxt + ") as T Where T.RowNum>" + (pagenum - 1) * pagesize + " And T.RowNum<=" + pagenum * pagesize;

        DataTable dt = new DataTable();
        dt = ExecuSQLtable(cmdTxt);

        return dt;
    }

    public DataTable ExecuSQLtable(string cmdtxt)
    {
        DataTable dt = new DataTable();
        using (SqlConnection conn = new SqlConnection(Class1.cnnstring))
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = cmdtxt;
                DbDataAdapter myadp = new SqlDataAdapter(cmd);
                myadp.Fill(dt);
            }

        }
        return dt;

    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // Response.Write(e.Row.Cells[0].Text);
        //Response.Write((FindControl("Button3") as Button).Text);

        if (e.Row.RowType != DataControlRowType.DataRow)
            return;
        if (e.Row.Cells[3].FindControl("HyperLink2")!=null)
        {
            HyperLink btn = e.Row.Cells[3].FindControl("HyperLink2") as HyperLink;
            if (btn != null)
            {

                btn.NavigateUrl = "delte_hope.aspx?id=" + e.Row.Cells[0].Text;
            
            }
        }
    }

    void Delete_hope_Command(object sender, CommandEventArgs e)
    {
        Response.Write(e.CommandName.ToString());
        //throw new NotImplementedException();
    }

    void btn1_Command(object sender, CommandEventArgs e)
    {
        Response.Write(e.CommandName.ToString());
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Init();
        DataLoad();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "")
        {
            cmdTxt = "Select Row_Number() Over (Order By ID) as RowNum,* From hopeperson where hopeid=@name";
            DataTable dt = Class1.exetable(cmdTxt, new SqlParameter("@name", TextBox1.Text));

            if (dt.Rows.Count != 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        else
        {
            Page.RegisterClientScriptBlock("aa", "<script language='javascript'>alert('查詢框不能為空!');</script>");
        }


    }
}

相關文章