GridView 自定義模版自定義修改,刪除,分頁
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));
}
}
相關文章
- 自定義GridViewView
- 自定義分頁格式
- 自定義分頁控制元件控制元件
- GridView自定義列資料繫結,和自定義頒功能View
- laravel model自定義軟刪除欄位Laravel
- 自定義RecyclerView實現側滑刪除View
- Javascript自定義陣列刪除方法remove()JavaScript陣列REM
- 自定義分頁標籤詳解
- python 自定義資料分頁Python
- 修改原始碼,自定義chromium啟動頁原始碼
- django 自定義分頁與bootstrap分頁結合Djangoboot
- XCode 建立自定義檔案模版XCode
- DataTables自定義分頁條數實現
- 自定義GridView實現分割線解析View
- 如何自定義 Django-allauth login模版Django
- lucene 自定義評分
- android 滑動刪除的listview(自定義view)AndroidView
- 使用 Laravel Resource 類時自定義分頁資訊Laravel
- XAF自定義啟動頁
- JavaWeb 自定義404頁面JavaWeb
- 自定義View:自定義屬性(自定義按鈕實現)View
- 刪除 IntelliJ Idea 中自定義的 Maven ArchetypeIntelliJIdeaMaven
- 新增了自定義的編輯和刪除按鈕
- win10如何刪除自定義的背景圖片 win10刪除背景自定義圖片歷史記錄的步驟Win10
- 如何自定義Safari的起始頁
- 自定義OAM錯誤頁面
- Swift - 自定義Share分享頁面Swift
- wordpress 自定義路由及展示頁路由
- 【WPF】自定義一個自刪除的多功能ListBox
- 自定義view之模擬qq訊息拖拽刪除效果View
- es 自定義分詞外掛分詞
- Excel--使用VBA Code 動態建立、修改和刪除自定義窗體程式碼摘抄Excel
- 08.Django自定義模板,自定義標籤和自定義過濾器Django過濾器
- VS2010自定義模版全過程
- Vue富文字帶圖片修改圖片大小自定義選擇項自定義字型Vue自定義字型
- 自定義ImageView完成圓形頭像自定義View
- 教你自定義Flutter錯誤頁面Flutter
- 前端頁面自定義滾動條前端