Webform GridView列表增删改查结合EF数据绑定
来源:互联网 发布:mysql 字符串截取 编辑:程序博客网 时间:2024/05/18 02:37
添加gridview数据到前端
<form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" Height="318px" Width="961px" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowCreated="GridView1_RowCreated"> <Columns> <asp:BoundField HeaderText="编号" DataField="Uid" ReadOnly="true" /> <asp:TemplateField HeaderText="姓名"> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"UserName") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"UserName") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="操作" ShowEditButton="true" ShowCancelButton="true" ShowDeleteButton="true" EditText="编辑" UpdateText="修改" DeleteText="<img style="text-decoration:none; border:0px;" src="images/delete.gif"onclick="JavaScript:return confirm ('确认删除吗?')" />" CancelText="取消" /> </Columns> </asp:GridView> <asp:Button ID="btnAdd" runat="server" Text="新增" OnClick="btnAdd_Click" /> </div> </form>
后端处理增删该查事件
CEducationEntities context = new CEducationEntities(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataBind(); } } //绑定GridView数据 private void DataBind() { List<User> users = context.User.Where(m => m.Uid != string.Empty).ToList(); this.GridView1.DataSource = users; this.GridView1.DataBind(); } //编辑选中列表 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { int index = e.NewEditIndex; GridView1.EditIndex = index; DataBind(); } //修改数据和插入更新 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int index = e.RowIndex; GridViewRow rows = this.GridView1.Rows[index]; string name = ((TextBox)rows.FindControl("txtName")).Text.Trim(); string ID = rows.Cells[0].Text; if (!string.IsNullOrEmpty(ID) && ID != " ") { var sc = context.User.First(p => p.Uid.Equals(ID)); sc.UserName = name; context.SaveChanges(); } else { char[] pattern = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g' }; int n = pattern.Length; string result = ""; Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < 4; i++) { int rnd = random.Next(0, n); result += pattern[rnd]; } User u = new User() { Uid = Guid.NewGuid().ToString(), Keyword = result, UserName = ((TextBox)rows.FindControl("txtName")).Text.Trim(), userLog = string.Empty }; context.User.Add(u); context.SaveChanges(); } this.GridView1.EditIndex = -1; DataBind(); } //取消修改 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; DataBind(); } //删除 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string strUid = GridView1.Rows[e.RowIndex].Cells[0].Text; User users = new User() { Uid = strUid }; context.User.Attach(users); context.User.Remove(users); context.SaveChanges(); DataBind(); } //新增按钮 protected void btnAdd_Click(object sender, EventArgs e) { List<User> list = context.User.Where(m => m.Uid != string.Empty).ToList(); User firstUser = new User(); list.Insert(list.Count, firstUser); this.GridView1.DataSource = list; this.GridView1.EditIndex = list.Count - 1; this.GridView1.DataBind(); } //创建新行 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType != DataControlRowType.EmptyDataRow) { e.Row.Cells[0].Enabled = false; } }
案例图片:
0 0
- Webform GridView列表增删改查结合EF数据绑定
- EF增删改查
- EF增删查改
- EF增删改查
- ef增删改查
- datalist绑定数据,实现增删改查
- EF操作增删改查
- EF的增删改查
- EF 的增删改查
- EF中的增删改查
- EF中增删改查
- EF实现增删改查
- GridView 增删改查
- EF总结--模型创建和数据的增删改查
- 关于EF学习,数据增删改查的讲解
- EF 通用数据层 增删改查操作,泛型类II
- Entity_Framework的增删改查,以及绑定GridView和TreeView
- ASP.NET实现增删改查等功能(Access版)系统之二-gridview绑定数据及行绑定
- 用户界面框架jQuery EasyUI示例大全之链接按钮
- maven install jar file to local repository
- Java程序10个面向对象设计原则
- Redis主从复制
- 越南机场被黑事件主角:1937CN Team宣布停止所有活动并关闭网站
- Webform GridView列表增删改查结合EF数据绑定
- n_multiple vectors and linearly independent
- 黄金平衡
- Android Broadcast Receiver 基础详解
- ssl证书的生成与签名
- linux--load解释
- 通过ffmpeg包制作GIF图片
- 线性代数学习笔记1
- 代码片段:基于 JDK 8 time包的时间工具类 TimeUtil