使用Gridview页脚添加记录

时间:2013-03-20 15:41:27

标签: c# asp.net gridview

我正在使用gridview的页脚添加新记录,尽管我不喜欢格式化。我希望能够使我的保存/取消按钮与编辑/删除命令字段一致。我无法弄清楚如何做到这一点。有人可以帮助。 干杯

<asp:GridView ID="Grd" runat="server" AutoGenerateColumns="false" OnRowEditing="Grd_RowEditing"
                                        OnRowCancelingEdit="Grd_RowCancelingEdit" OnRowUpdating="Grd_RowUpdating"
                                        Width="600px" CssClass="HomeDG" SelectedRowStyle-CssClass="HomeDGSelected" RowStyle-CssClass="HomeDGItem"
                                        AlternatingRowStyle-CssClass="HomeDGAlternating" HeaderStyle-CssClass="HomeDGhead"
                                        PageSize="10">
                                            <Columns>
                                                <asp:TemplateField HeaderText="Employee">
                                                    <ItemTemplate>
                                                        <asp:HiddenField ID="hfID" runat="server" Value='<%# Bind("Emp_ID")%>' />
                                                        <asp:Label ID="LblEmployee" runat="server" Text='<%# Bind("Emp_Name")%>'></asp:Label>
                                                    </ItemTemplate>
                                                    <FooterTemplate>
                                                        <asp:DropDownList ID="ddlNewEmp" runat="Server" />
                                                    </FooterTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Start Date">
                                                    <EditItemTemplate>
                                                        <asp:TextBox ID="TxtStartDate" runat="server" 
                                                        Text='<%#  Eval("Start_Date","{0:dd-MMM-yyyy}") %>'></asp:TextBox>
                                                       </EditItemTemplate>
                                                        <FooterTemplate>
                                                        <asp:TextBox ID="TxtStartDate" runat="Server" />
                                                        <asp:CalendarExtender ID="calStartDate" runat="server" TargetControlID="TxtStartDate"
                                                            Format="dd-MMM-yyyy" />
                                                        <asp:Button ID="btnAddNewSave" runat="server" Text="Save" CausesValidation="False"
                                                            OnClick="btnAddNewSave_Click" />
                                                        <asp:Button ID="btnAddNewCancel" runat="server" Text="Cancel" OnClick="btnAddNewCancel_Click" />
                                                    </FooterTemplate>
                                                </asp:TemplateField>
                                                <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" CausesValidation="False" />

                                            </Columns>
                                        </asp:GridView>
                                        <asp:Button ID="btnAddNew" runat="server" Text="Add" CausesValidation="False" OnClick="btnAddNew_Click" />

1 个答案:

答案 0 :(得分:0)

为网格添加RowCreated事件并在该处理程序中检查

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            var but = new LinkButton();
            but.Text = "Save";
            e.Row.Cells[0].Controls.Add(but);
        }