首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> ASP.NET技巧 -> asp.net 2.0中一次性更新所有GRIDVIEW的记录  
asp.net 2.0中一次性更新所有GRIDVIEW的记录


文章类别:ASP.NET技巧 来源: 作者: 发表日期:2005-10-4 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

  在asp.net 2.0中,gridview控件是十分不错的控件。有的时候,可能一个GRIDVIEW控件中的各行都是文本框,如何一次性更新所有修改过的记录呢?有两种方法,一种是使用sqldatasource来更新所有记录,但这个方法比较慢,因为每更新一条记录都要建立数据连接并执行updatecommand,会影响性能,但还是先来看下实现方法:
 
<%@ Page Language="C#" %>

<script runat="server">

    void Button1_Click(object sender, EventArgs e)

    {

        for (int i = 0; i < GridView1.Rows.Count; i++)

        {

            GridViewRow row = GridView1.Rows[i];

            SqlDataSource1.UpdateParameters[0].DefaultValue = ((TextBox)row.Cells[0].FindControl("TextBox2")).Text;

            SqlDataSource1.UpdateParameters[1].DefaultValue = ((TextBox)row.Cells[1].FindControl("TextBox3")).Text;

            SqlDataSource1.UpdateParameters[2].DefaultValue = GridView1.DataKeys[i].Value.ToString();

            SqlDataSource1.Update();

        }

    }   

   

</script>

<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="CustomerID"

            AutoGenerateColumns="False">

            <Columns>

                <asp:TemplateField SortExpression="CustomerID" HeaderText="CustomerID">

                <ItemTemplate>

                    <asp:TextBox Runat="server" Text='<%# Bind("CustomerID") %>' ID="TextBox1"></asp:TextBox>

                </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField SortExpression="CompanyName" HeaderText="CompanyName">

                    <ItemTemplate>

                        <asp:TextBox Runat="server" Text='<%# Bind("CompanyName") %>' ID="TextBox2"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField SortExpression="ContactName" HeaderText="ContactTitle">

                    <ItemTemplate>

                        <asp:TextBox Runat="server" Text='<%# Bind("ContactTitle") %>' ID="TextBox3"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>

        <asp:SqlDataSource ID="SqlDataSource1" Runat="server"

            SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]"

            UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactTitle] = @ContactTitle WHERE [CustomerID] = @CustomerID"

            ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>">

            <UpdateParameters>

                <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>

                <asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>

                <asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>

            </UpdateParameters>

        </asp:SqlDataSource>

        <asp:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" />&nbsp;

   

    </div>

    </form>

</body>

</html>

  另外一个方法是用组合SQL语句来进行的,速度比较快,原理也容易明白

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Text" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

   

    void Button1_Click(object sender, EventArgs e)

    {

        StringBuilder query = new StringBuilder();

       

        for (int i = 0; i < GridView1.Rows.Count; i++)

        {

            GridViewRow row = GridView1.Rows[i];

            string value1 = ((TextBox)row.Cells[0].FindControl("TextBox2")).Text.Replace("'","''");

            string value2 = ((TextBox)row.Cells[1].FindControl("TextBox3")).Text.Replace("'","''");

            string value3 = GridView1.DataKeys[i].Value.ToString();

 

            query.Append("UPDATE [Customers] SET [CompanyName] = '")

                .Append(value1).Append("' , [ContactTitle] = '")

                .Append(value2).Append("' WHERE [CustomerID] = '")

                .Append(value3).Append("';\n");

           

        }

 

        SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString);

        SqlCommand command = new SqlCommand(query.ToString(), con);

        con.Open();

        command.ExecuteNonQuery();

        con.Close();

    }

 

    void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString);

            SqlCommand command = new SqlCommand("SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]", con);

 

            con.Open();

            GridView1.DataSource = command.ExecuteReader();

            GridView1.DataBind();

            con.Close();

        }

    }

</script>

 

<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" DataKeyNames="CustomerID"

            AutoGenerateColumns="False">

            <Columns>

                <asp:TemplateField SortExpression="CustomerID" HeaderText="CustomerID">

                <ItemTemplate>

                    <asp:TextBox Runat="server" Text='<%# Bind("CustomerID") %>' ID="TextBox1"></asp:TextBox>

                </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField SortExpression="CompanyName" HeaderText="CompanyName">

                    <ItemTemplate>

                        <asp:TextBox Runat="server" Text='<%# Bind("CompanyName") %>' ID="TextBox2"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField SortExpression="ContactName" HeaderText="ContactTitle">

                    <ItemTemplate>

                        <asp:TextBox Runat="server" Text='<%# Bind("ContactTitle") %>' ID="TextBox3"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>

        <asp:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" />&nbsp;

    </div>

    </form>

</body>

</html>

上一篇:享受下载快感:比特精灵2.7详尽评测 下一篇:保存图片流到数据库之后固定显示新法
本栏目热门文章
·如何实现无刷新的DropdownList联动效果 2005-10-4
·使用HttpWebRequest向网站模拟上传数据 2005-10-4
·当DataSet中包含主/子表时,Update更新步骤 2005-10-6
·ASP.NET2.0实现无刷新客户端回调 2005-11-13
·ASP.NET中文件上传下载方法集合 2006-5-28
·分享个极好的无刷新二级联动下拉列表,同样适用与firefox 2005-10-19
·在Web DataGrid中当鼠标移到某行与离开时行的颜色发生改变( 2005-10-4
·ASP.NET中实现Flash与.NET的紧密集成 2005-11-21
·关于Asp.net页面Page_Load被执行两次的问题 2005-10-4
·ASP.NET极限:页面导航 (翻译) 2005-10-8
新近更新文章
·在VS2003中直接用DREAMWEAVER8打开ASPX文件 2006-6-22
·asp.net2.0中异步调用WebService(异步页) 2006-6-22
·ASP.NET2.0数据库入门之常见错误 2006-6-22
·ASP.NET 2.0中XSLT的使用 2006-6-22
·.Net2.0 使用ConfigurationManager读写配置 2006-6-22
·.net 做的IP 访问限制 2006-6-22
·ASP.Net2.0 GridView 多列排序,显示排序图标,分页 2006-6-22
·数据岛出到Excel最为简易的方法 2006-6-20
·权限树中Checkbox的操作[Asp.Net2.0] 2006-6-20
·使用Data Access Application Block 得到 2006-6-20
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: