1using System; 2using System.Data; 3using System.Configuration; 4using System.Collections; 5using System.Web; 6using System.Web.Security; 7using System.Web.UI; 8using System.Web.UI.WebControls; 9using System.Web.UI.WebControls.WebParts;10using System.Web.UI.HtmlControls;11using System.Collections.Generic;1213public partial class GridViewTemplateTest : System.Web.UI.Page14 {15 //当前页码,从0开始16 private int curPage17 {18 get19 {20 return ViewState["curPage"] == null ? 0 : Convert.ToInt32(ViewState["curPage"]);21 }22 set23 {24 ViewState["curPage"] = value;25 }26 }27 protected void Page_Load(object sender, EventArgs e)28 {29 if (!IsPostBack)30 {31 ClientInfoAccessObj accessor = new ClientInfoAccessObj();32 curPage = 0; //显示第一页 33 GridView1.DataSource = accessor.GetAllClients();34 GridView1.DataBind();35 }36 }37 protected void btnDeleteSelectedInfo_Click(object sender, EventArgs e)38 {39 List<int> SelectedClientIDs = GetSelectedClientIDs();40 if (SelectedClientIDs.Count == 0)41 return;4243 ClientInfoAccessObj accessor = new ClientInfoAccessObj();44 foreach (int id in SelectedClientIDs)45 {46 accessor.DeleteClientInfoForID(id);47 }48 ClientScript.RegisterStartupScript(this.GetType(), "info", "alert('记录已成功删除');", true);4950 //计算应该显示哪一页51 if (SelectedClientIDs.Count == GridView1.PageSize) //选中全页上的所有记录52 if (curPage != 0)53 curPage--; //显示前一页545556 //重新绑定数据并显示57 GridView1.DataSource = accessor.GetAllClients();58 GridView1.PageIndex = curPage;59 GridView1.DataBind();60 }6162 private List<int> GetSelectedClientIDs()63 {64 List<int> ids = new List<int>();65 //循环查找被选中的行66 foreach (GridViewRow gvr in GridView1.Rows) 67 {68 //是数据行69 if (gvr.RowType == DataControlRowType.DataRow)70 {71 //根据模板列中的控件ID查找指定的控件72 CheckBox chk = gvr.FindControl("CheckBox1") as CheckBox;73 if ((chk != null) && chk.Checked)74 //取出选中行的主键,加入到集合中75 ids.Add((int)GridView1.DataKeys[gvr.RowIndex].Value);76 }77 }78 return ids;79 }80 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)81 {82 ClientInfoAccessObj accessor = new ClientInfoAccessObj();83 GridView1.DataSource = accessor.GetAllClients();84 GridView1.PageIndex = e.NewPageIndex;85 GridView1.DataBind();86 //保存当前页码87 curPage = e.NewPageIndex;88 }89}90
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索web
, gridview
, checkbox学习
, viewstate
, 删除选中的多行数据?
, system
, datasource
, girdview
, item背景girdview
Accessor
,以便于您获取更多的相关知识。
时间: 2024-10-23 05:24:44