1、 数据库
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DisallowedName2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[DisallowedName2]
GO
CREATE TABLE [dbo].[DisallowedName2] (
[DisallowedNameID] [int] IDENTITY (1, 1) NOT NULL ,
[DisallowedName] [nvarchar] (256) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
2、 页面文件
<%@ Page Language="C#" CompileWith="Default7.aspx.cs" ClassName="Default7_aspx" %>
<%@ Page Language="C#" CompileWith="Default7.aspx.cs" ClassName="Default7_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="GridView1" runat="server" datasourceid="SqlDataSource1" datakeynames="DisallowedNameID"
autogeneratecolumns="False" showfooter="True" ondatabound="GridView1_DataBound" onrowcommand="GridView1_RowCommand">
<columns>
<asp:templatefield><edititemtemplate>
<asp:textbox id="TextBox2" runat="server" text='<%# Bind("DisallowedName") %>'></asp:textbox>
</edititemtemplate>
<footertemplate>
<asp:literal id="Literal2" runat="server" text="名称:"></asp:literal>
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<asp:linkbutton id="LinkButton1" runat="server" commandname="AddNewDisallowedName">添加</asp:linkbutton>
</footertemplate>
<itemtemplate>
<asp:literal id="Literal1" runat="server" text='<%# Eval("DisallowedName") %>'></asp:literal>
</itemtemplate>
</asp:templatefield>
<asp:commandfield showdeletebutton="True" showeditbutton="True"></asp:commandfield>
</columns>
</asp:gridview>
<asp:sqldatasource id="SqlDataSource1" runat="server" deletecommand="DELETE FROM [DisallowedName2] WHERE [DisallowedNameID] = @original_DisallowedNameID"
insertcommand="INSERT INTO [DisallowedName2] ([DisallowedName]) VALUES (@DisallowedName)"
selectcommand="SELECT [DisallowedNameID], [DisallowedName] FROM [DisallowedName2]"
updatecommand="UPDATE [DisallowedName2] SET [DisallowedName] = @DisallowedName WHERE [DisallowedNameID] = @original_DisallowedNameID"
connectionstring="<%$ ConnectionStrings:RuiYaDBConnection %>">
<deleteparameters>
<asp:parameter type="Int32" name="DisallowedNameID"></asp:parameter>
</deleteparameters>
<updateparameters>
<asp:parameter type="String" name="DisallowedName"></asp:parameter>
<asp:parameter type="Int32" name="DisallowedNameID"></asp:parameter>
</updateparameters>
<insertparameters>
<asp:parameter type="String" name="DisallowedName"></asp:parameter>
</insertparameters>
</asp:sqldatasource>
</div>
</form>
</body>
</html>
3、后如文件
using System;
using System.Data;
using System.Configuration;
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 Default7_aspx
{
void GridView1_DataBound(object sender, EventArgs e)
{
TableCellCollection tcc = GridView1.FooterRow.Cells;
int nTotalCols = tcc.Count;
for (int i = 0; i < nTotalCols - 1; i++)
{
GridView1.FooterRow.Cells.RemoveAt(1);
//此时只有一个单元格
TableCell c = GridView1.FooterRow.Cells[0];
c.ColumnSpan = nTotalCols;
}
}
void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddNewDisallowedName")
{
TextBox newDisallowedName = GridView1.FooterRow.FindControl("TextBox1") as TextBox;
SqlDataSource1.InsertParameters["DisallowedName"].DefaultValue = newDisallowedName.Text.Trim();
SqlDataSource1.Insert();
Response.Redirect(this.Request.RawUrl);
}
}
}