asp.net 2.0中一次性更新所有GRIDVIEW的记录
2024-07-10 12:57:07
供稿:网友
在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" />
</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" />
</div>
</form>
</body>
</html>
,欢迎访问网页设计爱好者web开发。