使用GridView添加,删除,编辑记录

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>&nbsp;
   
    </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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值