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();
            }
        }
  • 相关阅读:
    脚本绘图工具总结
    Windows Server 2012 磁盘管理之 简单卷、跨区卷、带区卷、镜像卷和RAID-5卷
    Java web项目 本地配置https调试
    Twitter的分布式自增ID算法snowflake (Java版)
    Redis压测命令
    Java读取txt文件信息并操作。
    安装配置adb工具及遇到的问题
    数据库逆向生成数据库表详细设计文档
    Java常用日期处理方法
    Date与String互相转换及日期的大小比较
  • 原文地址:https://www.cnblogs.com/stevenjson/p/3147388.html
Copyright © 2011-2022 走看看