GridView基于单元格的更新
2013-07-10 21:24
218 查看
CS代码:
[b]
[/b]
[b]难点ASP代码:[/b]
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using BookShopBLL; using BookShopModels; public partial class admin_UserList : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.GridView1.DataKeyNames = new string[] { "Id"};//设置绑定主键 this.GridView1.DataBind(); BindGridView(); //添加删除按钮的onclick代码 this.btnDelete.Attributes.Add("onclick"," return confirm('确定删除吗?')"); } } /// <summary> /// 绑定数据 /// </summary> private void BindGridView() { this.GridView1.DataSource = UsersManager.GetUser(); this.GridView1.DataBind(); } /// <summary> /// 在对行数据绑定后激发的事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //给删除按钮添加JavaScript脚本 LinkButton lnkbtnDel = e.Row.FindControl("lnkbtnDel") as LinkButton; lnkbtnDel.Attributes.Add("onclick", "return confirm('确认删除吗?')"); //光棒效果 e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor"); } //判断是否为空,如果不为空就绑定数据 if (e.Row.FindControl("ddlUserRole") != null) { DropDownList ddl = e.Row.FindControl("ddlUserRole") as DropDownList; ddl.DataSource = UserRoleManager.GetFindUserRole(); ddl.DataTextField = "Name"; ddl.DataValueField = "Id"; ddl.DataBind(); } } /// <summary> /// 根据用户类型Id获取用户类型名称 /// </summary> /// <param name="id"></param> /// <returns></returns> public string UserRolesName(string id) { UserRoles ur = UserRoleManager.GetUserRoleById(Convert.ToInt32(id)); return ur.Name; } /// <summary> /// 选择新行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { int id = e.NewSelectedIndex; string key = this.GridView1.DataKeys[id].Value.ToString();//获取选中行的主键 this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script>alert('"+key+"');</script>"); } /// <summary> /// 编辑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { this.GridView1.EditIndex = e.NewEditIndex;//设置当前编辑的行 BindGridView(); } /// <summary> /// 更新 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { Users user = new Users(); user.Id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString()); user.LoginId = (this.GridView1.Rows[e.RowIndex].FindControl("txtLoginId") as TextBox).Text; user.LoginPwd = (this.GridView1.Rows[e.RowIndex].FindControl("txtLoginPwd") as TextBox).Text; user.Name = (this.GridView1.Rows[e.RowIndex].FindControl("txtName") as TextBox).Text; user.Address = (this.GridView1.Rows[e.RowIndex].FindControl("txtAddress") as TextBox).Text; user.Phone = (this.GridView1.Rows[e.RowIndex].FindControl("txtPhone") as TextBox).Text; user.Mail = (this.GridView1.Rows[e.RowIndex].FindControl("txtEmail") as TextBox).Text; user.UserRole = new UserRoles(); user.UserRole.Id = Convert.ToInt32((this.GridView1.Rows[e.RowIndex].FindControl("ddlUserRole") as DropDownList).SelectedValue); user.Gender = (this.GridView1.Rows[e.RowIndex].FindControl("ddlGender") as DropDownList).Text; bool isUpdate = UsersManager.UpdateUser(user); if (isUpdate) { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script>alert('修改成功!')</script>"); this.GridView1.EditIndex = -1; BindGridView();//重新绑定 } else { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script>alert('修改失败!')</script>"); return; } } /// <summary> /// 取消编辑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { try { this.GridView1.EditIndex = -1;//退出编辑 BindGridView();//需要重新绑定数据 } catch (Exception) { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script>alert('操作失败!')</script>"); } } /// <summary> /// 删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { //找到当前删除行Id为lnkbtnDel的LinkButton控件 LinkButton lnkbtnDel=GridView1.Rows[e.RowIndex].FindControl("lnkbtnDel") as LinkButton; //取得删除行绑定的用户编号 int id = Convert.ToInt32(lnkbtnDel.CommandArgument); //调用删除方法 bool isDelete = UsersManager.DeleteUser(id); if (isDelete) { Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"info","<script>alert('删除成功!')</script>"); BindGridView();//重新绑定 } else { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script>alert('删除失败!')</script>"); } } /// <summary> /// 删除全部 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnDelete_Click(object sender, EventArgs e) { foreach (GridViewRow gr in this.GridView1.Rows)//循环每一行 { //获取模版列中的CheckBox控件对象 CheckBox chk = gr.FindControl("chbSelect") as CheckBox; if (chk.Checked) { //获得主键Id int id = Convert.ToInt32(this.GridView1.DataKeys[gr.RowIndex].Value); UsersManager.DeleteUser(id);//执行删除 } } BindGridView();//重新绑定 } }
[b]
[/b]
[b]难点ASP代码:[/b]
<asp:TemplateField HeaderText="邮箱"> <EditItemTemplate> <asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("Mail") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Mail") %>'></asp:Label> </ItemTemplate> <ControlStyle Width="130px" /> </asp:TemplateField> <asp:TemplateField HeaderText="用户类型"> <ItemTemplate> <asp:Label runat="server" ID="lblUserRole" Text='<%# UserRolesName(Eval("UserRole.Id").ToString()) %>' ></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlUserRole" runat="server" AutoPostBack="True"> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="性别"> <EditItemTemplate> <asp:DropDownList ID="ddlGender" runat="server"> <asp:ListItem Value="True" Text="男"></asp:ListItem> <asp:ListItem Value="False" Text="女"></asp:ListItem> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblGender" runat="server" Text='<%# Eval("Gender").ToString()=="True"?"男":"女" %>'></asp:Label> </ItemTemplate> <ControlStyle Width="50px" /> </asp:TemplateField> <asp:CommandField HeaderText="操作" ShowEditButton="True" /> <asp:TemplateField HeaderText="删除"> <ItemTemplate> <asp:LinkButton ID="lnkbtnDel" runat="server" Text="删除" CommandArgument='<%#Eval("Id") %>' CommandName="Delete"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="全选"> <HeaderTemplate> <input type="checkbox" onclick="GetAllCheckBox(this)" />全选 </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chbSelect" runat="server" /> </ItemTemplate> </asp:TemplateField>
相关文章推荐
- 新增格式化单元格数据功能-dotNetFlexGrid1.24,快速的设置您的表格数据的不同样式;现在开始扔掉你的Asp.net GridView吧。(更新FieldFormatorHandle参数)
- 用GridView编辑更新独立的单元格
- 用GridView编辑更新独立的单元格
- GridView更新某(个)些指定单元格以及在编辑模式下无刷新显示隐藏单元格内某控件
- 【转】用GridView编辑更新独立的单元格
- GridView更新某(个)些指定单元格以及在编辑模式下无刷新显示隐藏单元格内某控件
- 新增格式化单元格数据功能-dotNetFlexGrid更新1.24版本,快速的设置您的表格数据的不同样式;现在开始扔掉你的Asp.net GridView吧。
- 新增格式化单元格数据功能-dotNetFlexGrid1.24,快速的设置您的表格数据的不同样式;现在开始扔掉你的Asp.net GridView吧。(更新FieldFormatorHandle参数)
- GridView 加载空行并点击编辑每一个单元格
- GridView、ListView数据更新图片闪烁问题
- 使用gridview实现编辑、更新、取消功能和删除功能
- ASP.NET 2.0中合并 GridView 的表头单元格
- GridView单元格换行
- 基于C#实现的开源自动更新程序
- GridView实现双击进行编辑,更新
- ASP.NET 2.0中合并 GridView 的多表头单元格
- DevExpress gridview获取单元格坐标
- 关于ViewGroup 中listview 和GridView数据不能动态更新的解决
- C# .net Gridview 基于Jquery客户端 冻结行 冻结列 (固定行、固定列) js 插件