温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Handson简单新闻系统后台源码
当前文件:
HandsonNews/UserInfo/PopedomManage.aspx.cs,打开代码结构图
HandsonNews/UserInfo/PopedomManage.aspx.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.Data.SqlClient; 12
using System.Text; 13
14
public partial class UserInfo_PopedomManage : System.Web.UI.Page 15
{ 16
private string userId; 17
protected void Page_Load(object sender, EventArgs e) 18
{ 19
if (Request.Params["userid"] == null) 20
{ 21
22
} 23
else 24
{ 25
userId = Request.Params["userid"].ToString(); 26
} 27
} 28
29
protected void Btnsave_Click(object sender, EventArgs e) 30
{ 31
32
StringBuilder strSQL = new StringBuilder(); 33
using (SqlConnection conn = DataAccess.CreateConnection()) 34
{ 35
conn.Close(); 36
conn.Open(); 37
SqlCommand cmd = conn.CreateCommand(); 38
39
cmd.Parameters.Add("@USERID", SqlDbType.VarChar);//设定参数名 40
41
cmd.Parameters["@USERID"].Value = userId;//为参数赋值 42
43
44
strSQL.Append(" Delete from t_Popedom where USERID = @USERID ");//追加删除用户Sql语句 45
46
//从GridView控件中选出checkboxes控件 47
for (int i = 0; i < GridView1.Rows.Count; i++) 48
{ 49
GridViewRow row = GridView1.Rows[i]; 50
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked; 51
52
53
if (isChecked) 54
{ 55
//如果checkboxes被选中,追加insert语句添加用户权限 56
strSQL.Append(" Insert into t_Popedom (CLASSID, USERID) values (" + GridView1.Rows[i].Cells[0].Text + ", @USERID) "); 57
} 58
} 59
60
cmd.CommandText = strSQL.ToString(); 61
cmd.CommandTimeout = 300; 62
63
//启动事务处理 64
SqlTransaction sqlTran = conn.BeginTransaction(); 65
cmd.Transaction = sqlTran; 66
67
try 68
{ 69
cmd.ExecuteNonQuery(); 70
71
//提交事务 72
sqlTran.Commit(); 73
74
75
} 76
catch (Exception ex) 77
{ 78
//事务回滚 79
sqlTran.Rollback(); 80
81
//elMsg.InnerHtml = ex.ToString();//错误提示 82
83
} 84
finally 85
{ 86
sqlTran.Dispose();//结束事务,释放资源 87
88
conn.Close();//关闭数据库连接 89
90
91
92
} 93
} 94
} 95
protected void Btnback_Click(object sender, EventArgs e) 96
{ 97
Response.Redirect("UserManage.aspx"); 98
} 99
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 100
{ 101
if (e.Row.RowIndex != -1) 102
{ 103
string sql = "select * from t_Popedom where CLASSID=" + e.Row.Cells[0].Text + " and USERID=@USERID"; 104
SqlConnection con = DataAccess.CreateConnection(); 105
SqlCommand com = new SqlCommand(sql, con); 106
com.Parameters.AddWithValue("@USERID", userId); 107
con.Open(); 108
109
SqlDataReader dr = com.ExecuteReader(); 110
if (dr.Read()) 111
{ 112
CheckBox chk = (CheckBox)e.Row.Cells[4].FindControl("chkSelect"); 113
chk.Checked = true;//CheckBox 被选中 114
} 115
else 116
{ 117
CheckBox chk = (CheckBox)e.Row.Cells[4].FindControl("chkSelect"); 118
chk.Checked = false;//CheckBox 未被选中 119
} 120
121
} 122
} 123
} 124





}
}