GridView 自定義模版自定義修改,刪除,分頁

c979170768發表於2011-11-24
 
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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           Disp_Record(1);
        }
    

    }

    private void Disp_Record(int page_num)
    {
        string str_con = "server=.\\sqlexpress;initial catalog=student;integrated security=true ";
        using (SqlConnection con = new SqlConnection(str_con))
        {
            using (SqlCommand cmd = new SqlCommand("sp_Student_Select_by_Page_rowNumber", con))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add("@pagesize",1);
                cmd.Parameters.Add("@pagecount",  SqlDbType.Int).Direction = ParameterDirection.Output;
                cmd.Parameters.Add("@pageindex",page_num);
                DataSet ds = new DataSet();

                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    sda.Fill(ds);
                }   
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
                HiddenField1.Value = page_num.ToString();  //當前頁數
                   //HiddenField2.Value = cmd.Parameters["@pagecount"].Value.ToString();   //總頁數
                this.HiddenField2.Value = cmd.Parameters["@pagecount"].Value.ToString();
                Label1.Text = "當前記錄:" + HiddenField1.Value + "總記錄:" + HiddenField2.Value; ;
 
         
            }

        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Disp_Record(1);
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Disp_Record(Convert.ToInt32(HiddenField2.Value));
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int num=Convert.ToInt32(HiddenField1.Value);
        if (num>1)
        {
            Disp_Record(num-1); 
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {

        int num = Convert.ToInt32(HiddenField1.Value);
        if (num <Convert.ToInt32(HiddenField2.Value))
        {
            Disp_Record(num+1);
        }
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(TextBox8.Text)>0&&Convert.ToInt32(HiddenField2.Value)>=Convert.ToInt32(TextBox8.Text))
        {
            Disp_Record(Convert.ToInt32(TextBox8.Text));
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Disp_Record(Convert.ToInt32(HiddenField1.Value));

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
     
        TextBox id = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
        TextBox name = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;
        TextBox sex = GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox;
        TextBox age = GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox;
        TextBox pic = GridView1.Rows[e.RowIndex].FindControl("TextBox5") as TextBox;
        string str_con = "server=.\\sqlexpress;initial catalog=student;integrated security=true ";
        using (SqlConnection con=new SqlConnection(str_con))
        {
            using (SqlCommand cmd = new SqlCommand("update person set name=@name,sex=@sex,age=@age,pic=@pic where sid=@id",con))
            {
                cmd.Parameters.Add("@name",name.Text);
                cmd.Parameters.Add("@sex",sex.Text);
                cmd.Parameters.Add("@age",age.Text);
                cmd.Parameters.Add("@pic",pic.Text);
                cmd.Parameters.Add("@id",id.Text);
                con.Open();
                cmd.ExecuteNonQuery();
            }   
        }
        GridView1.EditIndex = -1;
        Disp_Record(Convert.ToInt32(HiddenField1.Value));
       
     
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Disp_Record(Convert.ToInt32(HiddenField1.Value));
    

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //TextBox id = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
        HiddenField row = GridView1.Rows[e.RowIndex].FindControl("HiddenField3") as HiddenField;
        string str_con = "server=.\\sqlexpress;initial catalog=student;integrated security=true ";
        using (SqlConnection con = new SqlConnection(str_con))
        {
            using (SqlCommand cmd = new SqlCommand("delete from person where id=@id", con))
            {
                cmd.Parameters.Add("@id", row.Value);
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
        GridView1.EditIndex = -1;
        Disp_Record(Convert.ToInt32(HiddenField1.Value));
        

     
    }
}


 

相關文章