1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11
12namespace bsTest2005_8_16.Samples
13{
14 /// <summary>
15 /// DataGrid翻页多选 的摘要说明。
16 /// </summary>
17 public class DataGrid翻页多选 : System.Web.UI.Page
18 {
19 protected System.Web.UI.WebControls.TextBox TextBox1;
20 protected System.Web.UI.WebControls.Button Button1;
21 protected System.Web.UI.WebControls.TextBox TextBox2;
22 protected System.Web.UI.WebControls.TextBox Textbox6;
23 protected System.Web.UI.WebControls.TextBox Textbox3;
24 protected System.Web.UI.WebControls.DataGrid DataGrid1;
25
26 private void Page_Load(object sender, System.EventArgs e)
27 {
28 if(!IsPostBack)
29 BindData();
30
31 this.Button1.Attributes.Add("onclick","return confirm('您确认要删除所有选中的记录吗?');");
32 }
33
34 private void BindData()
35 {
36 //模拟出一些原始数据绑定DataGrid
37 DataTable dt1 = new DataTable();
38 dt1.Columns.Add("field1");
39 dt1.Columns.Add("field2");
40 dt1.Columns.Add("field3");
41
42 Random radm = new Random();
43 for(int i=1;i<30;i++)
44 dt1.Rows.Add(new object[]{"id"+i.ToString(),(i+1)*(i+2)," ABC"+radm.Next(10000).ToString()});
45
46 this.DataGrid1.DataSource=dt1;
47 this.DataGrid1.DataBind();
48 }
49
50 private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
51 {
52 this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
53 BindData();
54 }
55
56
57 private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
58 {
59 if(e.Item.ItemType == ListItemType.Header)
60 {
61 CheckBox cb = e.Item.Cells[0].FindControl("CheckBoxAll") as CheckBox;
62 cb.Attributes.Add("onclick","selectAll(this);");//本页全选或取消的脚本函数
63 }
64 if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
65 {
66 CheckBox cb = e.Item.Cells[0].FindControl("CheckBox1") as CheckBox;
67 cb.Attributes.Add("onclick","selectOne(this);");//选中或取消当前行的脚本函数
68
69 //如果当前行已选,那么checkbox应该勾上
70 string curID = e.Item.Cells[1].Text;
71 if(this.TextBox1.Text.IndexOf(curID)>-1)
72 cb.Checked = true;
73 }
74 }
75
76 private void Button1_Click(object sender, System.EventArgs e)
77 {
78 string a = this.TextBox1.Text;
79 if(a.Length>0)
80 {
81 string sql = "delete from table1 where id in ("+a.TrimEnd(',')+")";
82
83 System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection("连接字符串");
84 System.Data.OleDb.OleDbCommand cm = new System.Data.OleDb.OleDbCommand(sql,cnn);
85 cnn.Open();
86 int deletedRowsCount = cm.ExecuteNonQuery();
87 cnn.Close();
88 }
89 }
90 Web 窗体设计器生成的代码
113
114 }
115}
116
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11
12namespace bsTest2005_8_16.Samples
13{
14 /// <summary>
15 /// DataGrid翻页多选 的摘要说明。
16 /// </summary>
17 public class DataGrid翻页多选 : System.Web.UI.Page
18 {
19 protected System.Web.UI.WebControls.TextBox TextBox1;
20 protected System.Web.UI.WebControls.Button Button1;
21 protected System.Web.UI.WebControls.TextBox TextBox2;
22 protected System.Web.UI.WebControls.TextBox Textbox6;
23 protected System.Web.UI.WebControls.TextBox Textbox3;
24 protected System.Web.UI.WebControls.DataGrid DataGrid1;
25
26 private void Page_Load(object sender, System.EventArgs e)
27 {
28 if(!IsPostBack)
29 BindData();
30
31 this.Button1.Attributes.Add("onclick","return confirm('您确认要删除所有选中的记录吗?');");
32 }
33
34 private void BindData()
35 {
36 //模拟出一些原始数据绑定DataGrid
37 DataTable dt1 = new DataTable();
38 dt1.Columns.Add("field1");
39 dt1.Columns.Add("field2");
40 dt1.Columns.Add("field3");
41
42 Random radm = new Random();
43 for(int i=1;i<30;i++)
44 dt1.Rows.Add(new object[]{"id"+i.ToString(),(i+1)*(i+2)," ABC"+radm.Next(10000).ToString()});
45
46 this.DataGrid1.DataSource=dt1;
47 this.DataGrid1.DataBind();
48 }
49
50 private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
51 {
52 this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
53 BindData();
54 }
55
56
57 private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
58 {
59 if(e.Item.ItemType == ListItemType.Header)
60 {
61 CheckBox cb = e.Item.Cells[0].FindControl("CheckBoxAll") as CheckBox;
62 cb.Attributes.Add("onclick","selectAll(this);");//本页全选或取消的脚本函数
63 }
64 if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
65 {
66 CheckBox cb = e.Item.Cells[0].FindControl("CheckBox1") as CheckBox;
67 cb.Attributes.Add("onclick","selectOne(this);");//选中或取消当前行的脚本函数
68
69 //如果当前行已选,那么checkbox应该勾上
70 string curID = e.Item.Cells[1].Text;
71 if(this.TextBox1.Text.IndexOf(curID)>-1)
72 cb.Checked = true;
73 }
74 }
75
76 private void Button1_Click(object sender, System.EventArgs e)
77 {
78 string a = this.TextBox1.Text;
79 if(a.Length>0)
80 {
81 string sql = "delete from table1 where id in ("+a.TrimEnd(',')+")";
82
83 System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection("连接字符串");
84 System.Data.OleDb.OleDbCommand cm = new System.Data.OleDb.OleDbCommand(sql,cnn);
85 cnn.Open();
86 int deletedRowsCount = cm.ExecuteNonQuery();
87 cnn.Close();
88 }
89 }
90 Web 窗体设计器生成的代码
113
114 }
115}
116