GridView 1 分页 全选 编辑 删除 更新
时间:2011-02-18 来源:happygx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowEditing="GridView1_RowEditing" DataKeyNames="ID" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField InsertVisible="False" ShowEditButton="True" />
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" Text="全选" OnCheckedChanged="CheckBox2_CheckedChanged"
AutoPostBack="true" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="序号" ReadOnly="true" />
<asp:BoundField DataField="num_iid" HeaderText="淘宝编号" ReadOnly="true" />
<asp:BoundField DataField="title" HeaderText="书名" />
<asp:BoundField DataField="nick" HeaderText="店铺" ReadOnly="true" />
<asp:BoundField DataField="num" HeaderText="数量" />
<asp:BoundField DataField="outer_id" HeaderText="商家编码" ReadOnly="true" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataLoad();
}
}
public void DataLoad()
{
string sql = "select ID,num_iid,title,nick,num,outer_id from dbo.TB_ItemsList_bak";
DataTable dt = DataDrive.sqlDataConn.connGetDataSetByExecSql(sql).Tables[0];
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
//分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
DataLoad();
}
//编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataLoad();
}
//取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataLoad();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
string s2 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string sql = string.Format("update tb_itemslist_bak set title='{0}' where ID={1} ", s2, id);
DataDrive.sqlDataConn.connGetDataSetByExecSql(sql);
GridView1.EditIndex = -1;
DataLoad();
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
string sql = string.Format("delete tb_itemslist_bak where id='{0}'", id);
DataDrive.sqlDataConn.connGetDataSetByExecSql(sql);
GridView1.EditIndex = -1;
DataLoad();
}
//全选
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
((CheckBox)(gvRow.Cells[0].FindControl("CheckBox1"))).Checked = ((CheckBox)sender).Checked;
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}}