GridView的Footer显示添加按钮

1、首先添加GridView
2、设置模板
<Columns>
                <asp:TemplateField HeaderText="账号">
                  <ControlStyle Width="100px" />
                  <ItemTemplate>
                      <asp:Label ID="lbID" runat="server" Text='<%# Bind("EmpID") %>'></asp:Label>
                  </ItemTemplate>
                  <FooterTemplate>
                      <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
                  </FooterTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="姓名">
                  <ControlStyle Width="100px" />
                  <ItemTemplate>
                      <asp:Label ID="lbRealName" runat="server" Text='<%# Bind("EmpRealName") %>'></asp:Label>
                  </ItemTemplate>
                  <FooterTemplate>
                      <asp:TextBox ID="txtRealName" runat="server"></asp:TextBox>
                  </FooterTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="性别">
                  <ItemTemplate>
                      <asp:Label ID="lbSex" runat="server" Text='<%# Bind("EmpSex") %>'></asp:Label>
                  </ItemTemplate>
                  <FooterTemplate>
                      <asp:DropDownList ID="ddlSex" runat="server">
                         <asp:ListItem Value="男">男</asp:ListItem>
                         <asp:ListItem Value="女">女</asp:ListItem>
                      </asp:DropDownList>
                  </FooterTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="住址">
                  <ControlStyle Width="200px" />
                  <ItemTemplate>
                      <asp:Label ID="lbAddress" runat="server" Text='<%# Bind("EmpAddress") %>'></asp:Label>
                  </ItemTemplate>
                  <FooterTemplate>
                      <asp:TextBox ID="txtAddress" runat="server" Width="80px"></asp:TextBox>
                      <asp:Button ID="btnAdd" runat="server" Text="添 加" OnClick="btnAdd_Click" />
                      <asp:Button ID="btnCancel" runat="server" Text="取 消" OnClick="btnCancel_Click" />
                  </FooterTemplate>
              </asp:TemplateField>
            </Columns>
3
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();

        }
    }
    public void bind()
    {
        string sqlStr = "select * from Employee";
        DataSet myds = Common.dataSet(sqlStr);
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "ID" };
        GridView1.DataBind();
    }
 
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        GridView1.ShowFooter = false;
        bind();
        //Functions.Alert("Cancel");
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        TextBox empID = GridView1.FooterRow.FindControl("txtID") as TextBox;
        TextBox empRealName = GridView1.FooterRow.FindControl("txtRealName") as TextBox;
        DropDownList empSex = GridView1.FooterRow.FindControl("ddlSex") as DropDownList;
        TextBox empAddress = GridView1.FooterRow.FindControl("txtAddress") as TextBox;
        string sql = "insert into Employee(EmpID,EmpRealName,EmpSex,EmpAddress) values('" + empID.Text.ToString() + "','" + empRealName.Text.ToString() + "','" + empSex.SelectedValue.ToString() + "','" + empAddress.Text.ToString() + "')";
        Common.ExecuteSql(sql);
        bind();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        GridView1.ShowFooter = true;
        bind();
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值