国家指南 ASP.NET项目开发指南:用户信息的处理(2)
ASP.NET项目开发指南:用户信息的处理(2)
用户信息的处理( )
删除
删除不会使控件处于编辑状态 所以只需要在这里得到用户单击的控件的某一项 然后用这一项和数据库进行关联 即可进行删除操作 删除完毕后不要忘记再读出数据 重新绑定到控件上 主要代码如程序 所示
程序 ST_Admin_userman aspx cs
protected void GridView _RowDeleting(object sender
GridViewDeleteEventArgs e)
{
string ST_myid;
string ST_strsql = ;
//获取当前行的主键
ST_myid = GridView Rows[e RowIndex] Cells[ ] Text;
//删除数据
ST_strsql = delete * fromt ST_tUser where ST_ID= + ST_myid;
ST_database execsql(ST_strsql)
//重新读取并绑定
ST_strsql = SELECT * FROM ST_tUser order by ID desc ;
DataTable ST_dt = ST_database ReadTable(ST_strsql)
GridView DataSource = ST_dt;
GridView DataBind()
}
【代码说明】代码第 行是获取GridView中要被删除的行的主键 因为前台在设计时将ID字段放在了第一列 而Cells[ ]就是读取第一列 因为索引是从 开始 删除数据后 GridView的数据发生变化 所以代码第 ~ 行需要重新读取数据库的数据并绑定到GridView
EditIndex属性
当单击 编辑 按钮时 需要先获取管理员所单击按钮在网格中的索引 然后将其赋值给GridView的EditIndex属性 再读取数据库 将数据绑定到GridView 主要代码如程序 所示
程序 ST_Admin_userman aspx cs
protected void GridView _RowEditing(object sender
GridViewEditEventArgs e)
{
//获取编辑的行号
GridView EditIndex = e NewEditIndex;
//重新绑定数据
string ST_strsql;
ST_strsql = SELECT * FROM ST_tUser order by ST_ID desc ;
DataTable ST_dt = ST_database ReadTable(ST_strsql)
GridView DataSource = ST_dt;
GridView DataBind()
}
【代码说明】代码第 行通过e NewEditIndex获取编辑的行号 因为页面的数据可能被用户作了更改 所以通过代码第 ~ 行重新绑定数据
更新
单击 更新 按钮会触发GridView _RowUpdating事件 在这个事件中 先退出编辑状态 然后根据管理员在TextBox中输入的数据更新数据库的内容 再次读取数据库中的内容 并绑定到GridView 主要代码如程序 所示
程序 ST_Admin_userman aspx cs
protected void GridView _RowUpdating(object sender
GridViewUpdateEventArgs e)
{
string ST_id;
string ST_strsql;
//定义 个TextBox控件
TextBox username userpass useracc;
ST_id = GridView Rows[e RowIndex] Cells[ ] Text;
username =
(TextBox)(GridView Rows[e RowIndex] Cells[ ] Controls[ ])
userpass =

(TextBox)(GridView Rows[e RowIndex] Cells[ ] Controls[ ])
useracc =
(TextBox)(GridView Rows[e RowIndex] Cells[ ] Controls[ ])
//更新数据库的数据
//TextBox Text=tb Text;
ST_strsql = update ST_tUser set ST_username= + username Text
+ ST_userpassword=
+ userpass Text + ST_userclass= + useracc Text
+ where ST_ID= + ST_id;
//Response Write(strsql)
ST_database execsql(ST_strsql)
//重新绑定GridView
ST_strsql = SELECT * FROM ST_tUser order by ST_ID desc ;
GridView EditIndex = ;
DataTable ST_dt = ST_database ReadTable(ST_strsql)
GridView DataSource = ST_dt;
GridView DataBind()
}
…
【代码说明】代码第 行定义了 个TextBox控件 它们的值通过代码第 ~ 行获取 其中e RowIndex表示当前编辑行的行号 而Cells[ ]就表示当前行的第 列 代码第 ~ 行是在数据库中更新当前行的语句 代码第 ~ 用来重新绑定更新后的数据
说明 Rows[e RowIndex] Cells[ ] Controls[ ]表示指定行第二列中的控件集合中的第一个控件
返回目录ASP NET项目开发指南
编辑推荐
ASP NET MVC 框架揭秘
ASP NET开发宝典
lishixinzhi/Article/program/net/201311/15927