网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.Asp.Net2.0中实现多任务异步页的.
.JSP与ASP.Net之间的Session值共享.
.ASP.Net C#2.0全能数据库组件 (开.
.在IIS6.0下ASP .NET 的版本冲突问.
.ASP.NET极限:页面导航 (翻译).
..net2.0邮件发送代码.
.ASP.NET服务器控件PleaseWaitBut.
.asp.net开发常用技巧收集.
.Asp.net(C#)多文件上传.
.將datagrid控件內容輸出到excel文.
.分析ASP.NET服务器控件开发-控件.
.在ASP.NET中实现多文件上传.
.GridViewRow可以任意位置单击引发.
.asp.net程序中最常用的三十三种编.
.五种常见的ASP.NET安全缺陷.
.在自定义HttpHandler中使用Sessi.
.asp.net2.0中关于ASP.NET 网站管.
.脚本获取选中文字及所在句子.
.如何防止页面中的敏感信息被提取.
.UrlRewriter重写地址的POSTBACK后.

asp.net 2.0中一次性更新所有GRIDVIEW的记录

发表日期:2004-10-22


  在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>

上一篇:保存图片流到数据库之后固定显示新法 人气:15847
下一篇:ASP.NET创建Web服务之设计方针 人气:10525
浏览全部asp.net的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐