当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net 2.0中一次性更新所有GRIDVIEW的记录

ASP.NET
ASP.NET中Session丢失原因与解决方案小结
.net开发中的一些注意事项及小技巧
学习Asp.Net经常会用到的函数集
在.net App中集成COM组件的一些简单技巧
彻底放弃IIS让Apache也支持ASP.NET
[JS.IntelliSense]VS2007(Orcas) So Cool
Asp.net 2.0 ViewState原理
asp.net ajax 使用updatepanel进行更新后的提示
动态代理DynamicProxy 介绍
您可能不知道的.Net2.0小技巧
Asp.Net2.0技巧(续)
“黑盒测试管理”以外的编程经验片断
实例开发:ASP.NET创建网络相册
封装stream,在读写stream时提供事件通知
GIS开发随笔--GIS技术的一点理解和MapNet控件试验
利用隐藏帧打印url的方法比较
无刷新仿google波形扭曲彩色Asp.net验证码
存储过程编写经验和优化措施
编程技巧:.Net Framework
编程技巧OOPs:复制构造函数

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 84 ::
收藏到网摘: n/a

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