GridView的使用-2

<form id="form1" runat="server">
    <div>
        <table>
            <tr><td  style="height: 39px; color: white; background-color: #003399;text-align:center"> 公司信息一览表</td></tr>
            <tr><td><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Width="694px">
            <Columns>
                <asp:TemplateField HeaderText="公司名称">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtCompanyName" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="200px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="140px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="联系人">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtLinkName" runat="server" Text='<%# Bind("Linkman") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Linkman") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="80px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="电话">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtTel" runat="server" Text='<%# Bind("Tel") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("Tel") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="140px" />
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField HeaderText="删除">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" CommandArgument='<%#Eval("Id") %>'>删除</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView></td></tr>
        </table>
       
        
    </div>
    </form>
  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = new CompanyManager().GetAllCompanys();
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("LinkButton1") as LinkButton).CommandArgument);
        if (new CompanyManager().DeleteCompany(id))
        { 
            Response.Write("<script>alert('删除成功')</script>");
            GridView1.DataSource = new CompanyManager().GetAllCompanys();
            GridView1.DataBind();
        }
        else
            Response.Write("<script>alert('删除失败')</script>");
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = new CompanyManager().GetAllCompanys();
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Company company = new Company();
        company.Id = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("LinkButton1") as LinkButton).CommandArgument);
        company.CompanyName = (GridView1.Rows[e.RowIndex].FindControl("txtCompanyName") as TextBox).Text;
        company.Linkman = (GridView1.Rows[e.RowIndex].FindControl("txtLinkName") as TextBox).Text;
        company.Tel = (GridView1.Rows[e.RowIndex].FindControl("txtTel") as TextBox).Text;
        company.Email = (GridView1.Rows[e.RowIndex].FindControl("txtEmail") as TextBox).Text;
        if (new CompanyManager().ModifyCompany(company))
        {
            Response.Write("<script>alert('修改成功')</script>");
            GridView1.EditIndex = -1;
            GridView1.DataSource = new CompanyManager().GetAllCompanys();
            GridView1.DataBind();
        }
        else
            Response.Write("<script>alert('修改失败')</script>");
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = new CompanyManager().GetAllCompanys();
        GridView1.DataBind();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值