GridView实现编辑删除
2013-06-20 22:56
183 查看
前台界面:
后台代码:
<asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False" AllowPaging="True" PageSize="12" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" DataKeyNames="id" OnPageIndexChanging="GridView1_PageIndexChanging" GridLines="None" BorderStyle="Groove"> <Columns> <asp:BoundField HeaderText="编号" DataField="id" Visible="false" /> <asp:BoundField HeaderText="序号" DataField="num" ReadOnly="True" /> <asp:BoundField DataField="name" HeaderText="隧道名称" ReadOnly="True" /> <asp:BoundField DataField="tunnelClassName" HeaderText="隧道类别" ReadOnly="True" /> <asp:TemplateField HeaderText="起始点"> <ItemTemplate> <%# Eval("PointStart")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="tbStart" Text='<%# Eval("PointStart") %>' runat="server" Width="90px" /> </EditItemTemplate> <ItemStyle Width="100px" /> </asp:TemplateField> <asp:TemplateField HeaderText="长度"> <ItemTemplate> <%# Eval("Length")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="tbLength" Text='<%# Eval("Length") %>' runat="server" Width="90px" /> </EditItemTemplate> <ItemStyle Width="100px" /> </asp:TemplateField> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" /> </Columns> <PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" /> <RowStyle Height="20px" BackColor="#FFF3EE" ForeColor="#333333" HorizontalAlign="Center"/> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#999999" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#FE6D38" ForeColor="Black" HorizontalAlign="Center" /> <HeaderStyle BackColor="#FE6D38" Font-Bold="True" ForeColor="Black"/> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView>
后台代码:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridViewBind(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; GridViewBind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); string connStr = ConfigurationManager.ConnectionStrings["NMXT_DBConn"].ConnectionString; string SqlStr = "delete from TN_TunnelBlankPoints where id=" + id; try { SqlConnection conn = new SqlConnection(connStr); if (conn.State.ToString() == "Closed") conn.Open(); SqlCommand comm = new SqlCommand(SqlStr, conn); comm.ExecuteNonQuery(); comm.Dispose(); if (conn.State.ToString() == "Open") conn.Close(); GridView1.EditIndex = -1; GridViewBind(); } catch (Exception ex) { Response.Write("数据库错误,错误原因:" + ex.Message); Response.End(); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; GridViewBind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); string strStart = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("tbStart")).Text; string strLength = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("tbLength")).Text; string connStr = ConfigurationManager.ConnectionStrings["NMXT_DBConn"].ConnectionString; string SqlStr = " update TN_TunnelBlankPoints set PointStart=" + strStart + ",Length=" + strLength + " where id=" + id; try { SqlConnection conn = new SqlConnection(connStr); if (conn.State.ToString() == "Closed") conn.Open(); SqlCommand comm = new SqlCommand(SqlStr, conn); comm.ExecuteNonQuery(); comm.Dispose(); if (conn.State.ToString() == "Open") conn.Close(); GridView1.EditIndex = -1; GridViewBind(); } catch (Exception ex) { Response.Write("数据库错误,错误原因:" + ex.Message); Response.End(); } }
相关文章推荐
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除 之代码规范
- GridView实现鼠标移动是,动态显示‘编辑、删除、查看’等命令按钮
- Repeater\DataList\GridView实现分页,数据编辑与删除
- 手动绑定数据到GridView并实现编辑,删除,取消···
- 手动绑定数据到GridView并实现编辑,删除,取消···
- GridView实现 选中,编辑,取消,删除
- Repeater\DataList\GridView实现分页,数据编辑与删除
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除、导出、突出显示等常见应用实现
- gridview 实现自定义分页、排序、查询、添加、编辑、多选删除 之代码规范
- GridView中用CommandField实现简单的编辑(更新,取消),删除
- 如何利用GridView与UpdatePanel组合实现无刷新:新增、编辑、删除、分页效果?
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除 之代码规范
- GridView 中如何实现编辑 删除(在编辑状态中 嵌套DropDownList)
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除、导出、突出显示等常见应用实现
- 使用gridview实现编辑、更新、取消功能和删除功能
- 图片实现gridview编辑删除事件
- 手工实现GridView排序、删除、编辑、新增数据功能
- GridView 编辑和删除的实现
- 使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除