您的位置:首页 > 其它

GridView CheckBox当翻页时保存选中的项(viewstate)

2007-11-20 13:26 429 查看
1



属性 保存全选的项#region 属性 保存全选的项
2


3

protected ArrayList SelectedItems
4





{
5

get
6





{
7

if (ViewState["SelectedItems"] == null)
8





{
9

ViewState["SelectedItems"] = new ArrayList();
10

}
11


12

return ViewState["SelectedItems"] as ArrayList;
13

}
14

set
15





{
16

ViewState["SelectedItems"] = value;
17

}
18

}
19



/**//// <summary>
20

/// 页面加载
21

/// </summary>
22

/// <param name="sender"></param>
23

/// <param name="e"></param>
24

protected void Page_Load(object sender, EventArgs e)
25





{
26

SaveSelectedItems();
27

if (!IsPostBack)
28





{
29

if (myGridView.Attributes["SortExpression"] == null)
30





{
31

myGridView.Attributes["SortExpression"] = "DAID";
32

myGridView.Attributes["SortDirection"] = "asc";
33

}
34

BindDynamicAccountFlowInfo();
35

}
36

}
37



/**//// <summary>
38

/// 保存页面选中的项
39

/// </summary>
40

private void SaveSelectedItems()
41





{
42

for (int i = 0; i < myGridView.Rows.Count; i++)
43





{
44

CheckBox tempCheckBox = myGridView.Rows[i].FindControl("chkSelect") as CheckBox;
45

string id = myGridView.Rows[i].Cells[0].Text;
46


47

if (SelectedItems.Contains(id) && !tempCheckBox.Checked)
48





{
49

SelectedItems.Remove(id);
50

}
51

if (!SelectedItems.Contains(id) && tempCheckBox.Checked)
52





{
53

SelectedItems.Add(id);
54

}
55

}
56

}
57



/**//// <summary>
58

/// 行绑定
59

/// </summary>
60

/// <param name="sender"></param>
61

/// <param name="e"></param>
62

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
63





{
64

if (e.Row.RowType == DataControlRowType.DataRow)
65





{
66

if (e.Row.RowIndex > -1 && SelectedItems.Count > 0)
67





{
68

CheckBox tempCheckBox = e.Row.FindControl("chkSelect") as CheckBox;
69

string id = e.Row.Cells[0].Text;
70

if (SelectedItems.Contains(id))
71





{
72

tempCheckBox.Checked = true;
73

}
74

else
75





{
76

tempCheckBox.Checked = false;
77

}
78

}
79

}
80


81

}
82



/**//// <summary>
83

/// 删除按钮
84

/// </summary>
85

/// <param name="sender"></param>
86

/// <param name="e"></param>
87

protected void btnDelAll_ServerClick(object sender, EventArgs e)
88





{
89

string sID = "";//选中删除所有行的id
90


91

foreach (string tempString in SelectedItems)
92





{
93

sID += tempString + ",";
94

}
95

if (SelectedItems.Count < 1)
96





{
97

ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('你没有选择!!!')</script>");
98

}
99

else
100





{
101

daDB.DelDynamicAccountFlowinfo(sID.Substring(0, sID.Length - 1));
102

}
103

if (Cache["dataSource"] != null)
104

Cache.Remove("dataSource");//***先清空原有页面缓存
105


106

BindDynamicAccountFlowInfo();
107

}
108

下面是删除操作代码(DB):

1

public bool DelDynamicAccountFlowinfo(string sID)
2





{
3

string sqlstr=string.Format("delete from DynamicAccountFlow where DAFID IN({0})", sID);
4

conn = Myconn.SqlConnection;
5

SqlCommand mycommand = new SqlCommand(sqlstr, conn);
6

mycommand.CommandType = CommandType.Text;
7

mycommand.CommandText = sqlstr;
8

conn.Open();
9

mycommand.ExecuteNonQuery();
10

conn.Close();
11

return true;
12

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: