zoukankan      html  css  js  c++  java
  • GridView实现编辑删除

    前台界面:

      <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();
            }
        }
  • 相关阅读:
    Java WebService异构系统通信的原理及特点:SOAP与WSDL
    jenkins下拉框选择构建环境
    vue中的hash与history
    一行代码轻松搞定企微内嵌h5调用原生api不生效问题
    开源绘图工具plantUML入门教程(常用于画时序图等)
    什么是持续集成、持续交付、持续部署(CI/CD)?
    一篇文章了解CI/CD管道全流程
    开源免费的SSH工具推荐:electerm(推荐)、Finalshell
    Oracle数据库设置表空间自动扩展(解决因表空间不足引起的ORA01653: unable to extend table错误)
    测试工作中浏览器F12工具简单使用介绍
  • 原文地址:https://www.cnblogs.com/stevenjson/p/3147388.html
Copyright © 2011-2022 走看看