您的位置:首页 > 移动开发 > Objective-C

GridView的Footer显示添加按钮

2010-09-01 11:32 316 查看
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();
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐