• 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();
            }
        }
  • 相关阅读:
    python的函数
    Python的条件语句和循环语句
    Python的输入与输出
    Python变量和类型
    Python的运算符
    Python的注释
    pycharm基本使用
    推特史上最大规模黑客入侵案:17岁问题少年的隐秘人生
    进程和线程的区别及线程的介绍
    python接口自动化42
  • 原文地址:https://www.cnblogs.com/stevenjson/p/3147388.html
Copyright © 2020-2023  润新知