• GridView的修改,删除,查询


    前台代码:

      <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="DepartId">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            部门编号
                        </HeaderTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DepartId") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("DepartId") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            部门名称
                        </HeaderTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("DepartName") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("DepartName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="操作" ShowHeader="False">
                        <EditItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="更新"></asp:LinkButton>
                             <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑"></asp:LinkButton>
                            <asp:LinkButton ID="LinkButton2" CausesValidation="false" CommandName="Delete" Text="删除" runat="server" />                     
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    

    后台代码:

    public partial class WebForm3 : System.Web.UI.Page
        {
            DepartmentInfoBLL dbll = new DepartmentInfoBLL();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    InitGridView();
                }
            }
    
            private void InitGridView()
            {
                IList<DepartmentInfo> list = dbll.GetAll();
                this.GridView1.DataSource = list;
                this.GridView1.DataBind();
            }
    
            protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                int departId = int.Parse(e.Keys[0].ToString());
                //执行删除的方法
                dbll.DeleteByDepartId(departId);
                //重新加载数据
                InitGridView();
            }
    
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                this.GridView1.EditIndex = e.NewEditIndex;
                InitGridView();
            }
    
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                GridViewRow row = this.GridView1.Rows[e.RowIndex];
                int departId = int.Parse(e.Keys[0].ToString());
                string departName = (row.FindControl("TextBox2") as TextBox).Text;
                //执行修改的方法
                dbll.Update(new DepartmentInfo()
                {
                    DepartId = departId,
                    DepartName = departName,
                    Remark= ""
                });
                //取消编辑状态
                this.GridView1.EditIndex = -1;
                //重新加载一次数据
                InitGridView();
            }
    
            protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                this.GridView1.EditIndex = -1;
                InitGridView();
            }
        }
    

      

  • 相关阅读:
    。。。
    __new__ 单例
    bokeh
    空间数据可视化
    关系网络图
    Pandas 50题练习
    seaborn
    数据输出及内容美化 简单介绍
    数据分析---项目总结
    数学建模
  • 原文地址:https://www.cnblogs.com/liujie1111/p/3648469.html
Copyright © 2020-2023  润新知