aspx <%@ Page Language="C#" Theme="" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %><!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" xml:lang="en" lang="en"> <head id="Head1" runat="server"> <title>ASP.NET Insert data in Gridview </title> </head> <body> <form id="form1" runat="server"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:GridView ID="GridView1" ShowFooter="true" runat="server" OnRowCommand="GridView1_RowCommand1" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Button Text="Edit" CommandName="Edit" CausesValidation="false" runat="server" ID="btEdit" /> <asp:Button Text="Delete" CommandName="Delete" CausesValidation="false" runat="server" ID="btDelete" /> </ItemTemplate> <EditItemTemplate> <asp:Button Text="Update" CommandName="Update" CausesValidation="true" runat="server" ID="btUpdate" /> <asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" /> </EditItemTemplate> <FooterTemplate> <asp:Button Text="Insert" CommandName="Insert" CausesValidation="true" runat="server" ID="btInsert" /> <asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" /> </FooterTemplate> </asp:TemplateField> <asp:TemplateField > <ItemTemplate> <asp:Label ID="lblValue" Text='<%# Eval("Name") %>' runat="server"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="tbUpdate" runat="server" Text='<% Bind("Name") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="tbInsert" runat="server" Text="" ></asp:TextBox> </FooterTemplate> </asp:TemplateField> </Columns> <EmptyDataTemplate> <asp:TextBox ID="tbEmptyInsert" runat="server"></asp:TextBox><br /> <asp:Button ID="btSend" Text="Insert" runat="server" CommandName="EmptyInsert" UseSubmitBehavior="False" /> </EmptyDataTemplate> </asp:GridView> </form> </body> </html> aspx.cs public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //Create dummy data DataTable dt = new DataTable(); DataColumn dc = new DataColumn("Name"); dt.Columns.Add(dc); DataRow dr = dt.NewRow(); dr["Name"] = "Ivan"; //Uncomment the following line to have data in the grid :) //dt.Rows.Add(dr); //Bind the gridview GridView1.DataSource = dt; GridView1.DataBind(); } //Recurses through the controls to show the naming of each individual control that is currently in the gridview RecurseControls(GridView1.Controls[0].Controls); Label1.Text += GridView1.Controls[0].Controls[0].GetType().Name + "<br />"; } void RecurseControls(ControlCollection ctls) { foreach (Control ctl in ctls) { if (!ctl.HasControls()) Label1.Text += ctl.ClientID + " " + ctl.GetType().Name + "<br />"; else RecurseControls(ctl.Controls); } }
protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "EmptyInsert") { //handle insert here TextBox tbEmptyInsert = GridView1.Controls[0].Controls[0].FindControl("tbEmptyInsert") as TextBox; Label1.Text = string.Format("You would have inserted the name : <b>{0}</b> from the emptydatatemplate",tbEmptyInsert.Text); } if (e.CommandName == "Insert") { //handle insert here TextBox tbInsert = GridView1.FooterRow.FindControl("tbInsert") as TextBox; Label1.Text = string.Format("You would have inserted the name : <b>{0}</b> from the footerrow", tbInsert.Text); } } }
你可以用代码连接数据库,再插入记录.
问题是,你的commandName="Edit"会出现edit模板.
但是却没有insert模板.
或许是我不知道,网上也没有找到.
<%@ Page Language="C#" Theme="" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %><!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" xml:lang="en" lang="en">
<head id="Head1" runat="server">
<title>ASP.NET Insert data in Gridview </title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:GridView ID="GridView1" ShowFooter="true" runat="server"
OnRowCommand="GridView1_RowCommand1" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Edit" CommandName="Edit" CausesValidation="false" runat="server" ID="btEdit" />
<asp:Button Text="Delete" CommandName="Delete" CausesValidation="false" runat="server" ID="btDelete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button Text="Update" CommandName="Update" CausesValidation="true" runat="server" ID="btUpdate" />
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button Text="Insert" CommandName="Insert" CausesValidation="true" runat="server" ID="btInsert" />
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:Label ID="lblValue" Text='<%# Eval("Name") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbUpdate" runat="server" Text='<% Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbInsert" runat="server" Text="" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:TextBox ID="tbEmptyInsert" runat="server"></asp:TextBox><br />
<asp:Button ID="btSend" Text="Insert" runat="server" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>
</form>
</body>
</html>
aspx.cs
public partial class Test : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ //Create dummy data DataTable dt = new DataTable();
DataColumn dc = new DataColumn("Name");
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr["Name"] = "Ivan";
//Uncomment the following line to have data in the grid :)
//dt.Rows.Add(dr);
//Bind the gridview
GridView1.DataSource = dt; GridView1.DataBind();
}
//Recurses through the controls to show the naming of each individual control that is currently in the gridview
RecurseControls(GridView1.Controls[0].Controls);
Label1.Text += GridView1.Controls[0].Controls[0].GetType().Name + "<br />";
} void RecurseControls(ControlCollection ctls)
{
foreach (Control ctl in ctls)
{
if (!ctl.HasControls())
Label1.Text += ctl.ClientID + " " + ctl.GetType().Name + "<br />";
else
RecurseControls(ctl.Controls);
}
}
protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EmptyInsert")
{
//handle insert here
TextBox tbEmptyInsert = GridView1.Controls[0].Controls[0].FindControl("tbEmptyInsert") as TextBox;
Label1.Text = string.Format("You would have inserted the name : <b>{0}</b> from the emptydatatemplate",tbEmptyInsert.Text); }
if (e.CommandName == "Insert")
{
//handle insert here
TextBox tbInsert = GridView1.FooterRow.FindControl("tbInsert") as TextBox;
Label1.Text = string.Format("You would have inserted the name : <b>{0}</b> from the footerrow", tbInsert.Text);
}
}
}