代码之家  ›  专栏  ›  技术社区  ›  J.C

如何在ASP中通过列表填充的gridview中添加页脚行。网

  •  0
  • J.C  · 技术社区  · 7 年前

    我需要填充一个 GridView 我想添加一个页脚行,这样用户就可以添加一个新行。

    这就是我的前端。

       <asp:GridView ID="GridView1" AllowPaging="true"  ShowFooter="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing"
            runat="server"  CellPadding="3"  GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
             OnRowCancelingEdit="GridView1_RowCancelingEdit">
            <Columns>            
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px"/>
                            <asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px"/>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px"/>
                            <asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:ImageButton ImageUrl="~/Images/addnew.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px"/>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
       </asp:GridView>  
    

    这就是我如何填充 GridView .

        protected List<Emp> GetEmpList()
        {
            List<Emp> lEmp = new List<Emp>();
            Emp oemp = new Emp(1234, "Upendra", "Noida");
            lEmp.Add(oemp);
            oemp = new Emp(1934, "Rahul", "Noida");
            lEmp.Add(oemp);
            //.......
            return lEmp;
        }
    
        protected void BindGridList()
        {
            GridView1.DataSource = GetEmpList();
            GridView1.DataBind();
        }
    

    这是我的网格图。 My datagrid

    我的问题是如何添加页脚行谢谢你的帮助。

    0 回复  |  直到 7 年前
        1
  •  2
  •   Skaria Thomas    7 年前

    我们需要修改以下内容以在GridView中实现页脚控件。

    1) GridView-更改为AutoGenerateColumns=“False” 2) 使用TemplateField将所有柱添加到设计视图中的网格中 3) 描述列的EditItemTemplate、ItemTemplate和FooterTemplate 4) 在代码隐藏文件中,使用GridView1访问这些值。页脚行。FindControl

    ASPX代码示例

    <asp:GridView ID="GridView1" AllowPaging="True" ShowFooter="True" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing"
                    runat="server" CellPadding="3" GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
                    OnRowCancelingEdit="GridView1_RowCancelingEdit" AutoGenerateColumns="False">
    <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px" />
                                <asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px" />
                                <asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px" />
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:ImageButton ImageUrl="~/Images/addnew.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px" OnClick="btnAddNew_Click" />
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Id">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtNameFooter" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="City">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("City") %>'></asp:Label>
                            </ItemTemplate>
                             <FooterTemplate>
                                <asp:TextBox ID="txtCityFooter" runat="server"></asp:TextBox>
                            </FooterTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
    

    示例代码隐藏代码:

    protected void btnAddNew_Click(object sender, EventArgs e)
            {
                TextBox txtName = GridView2.FooterRow.FindControl("txtNameFooter") as TextBox;
                TextBox txtCity = GridView2.FooterRow.FindControl("txtCityFooter") as TextBox;
                cmd.CommandText = $"INSERT INTO tblLocation(Name,City) VALUES('{txtName.Text}','{txtCity.Text}')";
            }
    
    推荐文章