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

简介:

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


<%@PageLanguage="C#"%>

<scriptrunat="server">

voidButton1_Click(objectsender,EventArgse)

{

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

{

GridViewRowrow=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>

<htmlxmlns="
<headrunat="server">

<title>UntitledPage</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:GridViewID="GridView1"Runat="server"DataSourceID="SqlDataSource1"DataKeyNames="CustomerID" AutoGenerateColumns="False">

<Columns>

<asp:TemplateFieldSortExpression="CustomerID"HeaderText="CustomerID">

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateFieldSortExpression="CompanyName"HeaderText="CompanyName">

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateFieldSortExpression="ContactName"HeaderText="ContactTitle">

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

<asp:SqlDataSourceID="SqlDataSource1"Runat="server"

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

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

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

<UpdateParameters>

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

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

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

</UpdateParameters>

</asp:SqlDataSource>

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

</div>

</form>

</body>

</html>

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


<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Text"%>
<%@ImportNamespace="System.Data.SqlClient"%>
<scriptrunat="server">
voidButton1_Click(objectsender,EventArgse)
{
StringBuilderquery=newStringBuilder();
for(inti=0;i<GridView1.Rows.Count;i++)
{
GridViewRowrow=GridView1.Rows[i];
stringvalue1=((TextBox)row.Cells[0].FindControl("TextBox2")).Text.Replace("'","''");
stringvalue2=((TextBox)row.Cells[1].FindControl("TextBox3")).Text.Replace("'","''");
stringvalue3=GridView1.DataKeys[i].Value.ToString();
query.Append("UPDATE[Customers]SET[CompanyName]='")
.Append(value1).Append("',[ContactTitle]='")
.Append(value2).Append("'WHERE[CustomerID]='")
.Append(value3).Append("';/n");
}
SqlConnectioncon=newSqlConnection(ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString);
SqlCommandcommand=newSqlCommand(query.ToString(),con);
con.Open();
command.ExecuteNonQuery();
con.Close();
}
voidPage_Load(objectsender,EventArgse)
{
if(!Page.IsPostBack)
{
SqlConnectioncon=newSqlConnection(ConfigurationSettings.ConnectionStrings["AppConnectionString1"].ConnectionString);
SqlCommandcommand=newSqlCommand("SELECT[CustomerID],[CompanyName],[ContactName],[ContactTitle]FROM[Customers]",con);
con.Open();
GridView1.DataSource=command.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>UntitledPage</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="GridView1"Runat="server"DataKeyNames="CustomerID"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateFieldSortExpression="CustomerID"HeaderText="CustomerID">
<ItemTemplate>
<asp:TextBoxRunat="server"Text='<%#Bind("CustomerID")%>'ID="TextBox1"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldSortExpression="CompanyName"HeaderText="CompanyName">
<ItemTemplate>
<asp:TextBoxRunat="server"Text='<%#Bind("CompanyName")%>'ID="TextBox2"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateFieldSortExpression="ContactName"HeaderText="ContactTitle">
<ItemTemplate>
<asp:TextBoxRunat="server"Text='<%#Bind("ContactTitle")%>'ID="TextBox3"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ButtonID="Button1"Runat="server"Text="Button"OnClick="Button1_Click"/>&nbsp;
</div>
</form>
</body>
</html>
目录
相关文章
|
2月前
|
SQL 开发框架 前端开发
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
34 0
|
2月前
|
SQL 开发框架 .NET
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
33 0
|
开发框架 .NET 数据库连接
在ASP.NET中实现选中、编辑和删除GridView数据项
在ASP.NET中实现选中、编辑和删除GridView数据项
在ASP.NET中实现选中、编辑和删除GridView数据项
|
.NET 开发框架 索引
asp.net给Reaper和GridView添加序号
repeater添加序号列的方法 1、     2、     3、在中添加   function show() { var bj = document.all.tags("LABEL"); for (i=0;i   ASP.
904 0
|
JavaScript 前端开发 .NET
关于asp:GridView和dx:ASPxGridView固定表头的jquery代码封装
前几天有个项目要实现dx:ASPxGridView固定表头,就翻看了网上实现的方法。总结了一些方法。废话不多,先上个图,有图有真相           图1,dx:ASPxGridView的上面还有其他元素     图2    这是基于dx:ASPxGridView固定表头。
1225 0
|
Web App开发 SQL 前端开发
Asp.net中GridView使用详解(引)
GridView无代码分页排序 GridView选中,编辑,取消,删除 GridView正反双向排序 GridView和下拉菜单DropDownList结合 GridView和CheckBox结合 鼠标移到GridView某一行时改变该行的背景色方法一 鼠标移到GridView某一行时改...
1185 0
|
Web App开发 SQL 前端开发
Asp.net中GridView使用详解(引)
GridView无代码分页排序 GridView选中,编辑,取消,删除 GridView正反双向排序 GridView和下拉菜单DropDownList结合 GridView和CheckBox结合 鼠标移到GridView某一行时改变该行的背景色方法一 鼠标移到GridView某一行时改...
953 0
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
41 0