示例效果如下
点击 新增 按钮 Grid将增加一空行 用于输入
点击 删除 按钮 将删除该行记录并重新绑定Grid
相关代码如下
HTML部分
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="PrivateWork_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Grid 新增及删除一行</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="pID">
<ItemTemplate>
<asp:TextBox ID="txt_pID" runat="server" Width="98"
Text='<%# Bind("pID") %>'></asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="pName">
<ItemTemplate>
<asp:TextBox ID="txt_pName" runat="server" Width="98"
Text='<%# Bind("pName") %>' ></asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="btn_NewAdd" runat="server" Text="新增"
OnClick="btn_NewAdd_Click" />
</HeaderTemplate>
<ItemTemplate>
<asp:Button ID="btn_Delete" runat="server" Text="删除"
CommandArgument="<%# GridView1.Rows.Count %>"
OnClick="btn_Delete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btn_SaveAndShow" runat="server" Text="保存并显示数据在GridView2"
OnClick="btn_SaveAndShow_Click" /><br />
<asp:GridView ID="GridView2" runat="server" Width="200">
</asp:GridView>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Grid 新增及删除一行</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="pID">
<ItemTemplate>
<asp:TextBox ID="txt_pID" runat="server" Width="98"
Text='<%# Bind("pID") %>'></asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="pName">
<ItemTemplate>
<asp:TextBox ID="txt_pName" runat="server" Width="98"
Text='<%# Bind("pName") %>' ></asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="btn_NewAdd" runat="server" Text="新增"
OnClick="btn_NewAdd_Click" />
</HeaderTemplate>
<ItemTemplate>
<asp:Button ID="btn_Delete" runat="server" Text="删除"
CommandArgument="<%# GridView1.Rows.Count %>"
OnClick="btn_Delete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btn_SaveAndShow" runat="server" Text="保存并显示数据在GridView2"
OnClick="btn_SaveAndShow_Click" /><br />
<asp:GridView ID="GridView2" runat="server" Width="200">
</asp:GridView>
</form>
</body>
</html>
后台部分
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class PrivateWork_Default : System.Web.UI.Page
{
Page_Load 构造DataTable
Grid 新增一行
Grid 删除一行
保存并显示 按钮
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class PrivateWork_Default : System.Web.UI.Page
{
Page_Load 构造DataTable
Grid 新增一行
Grid 删除一行
保存并显示 按钮
}