国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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" />&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>



,歡迎訪問網頁設計愛好者web開發。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 克拉玛依市| 石首市| 沈丘县| 揭阳市| 甘泉县| 丰都县| 大方县| 临西县| 开化县| 阆中市| 灯塔市| 洪泽县| 绍兴市| 嵊州市| 日土县| 荃湾区| 镶黄旗| 青海省| 虎林市| 石渠县| 杭锦后旗| 德庆县| 定州市| 衡南县| 玉龙| 长治县| 洞口县| 荥阳市| 达孜县| 柞水县| 曲阳县| 黄龙县| 柘城县| 荣昌县| 大英县| 乌拉特前旗| 水城县| 江达县| 六盘水市| 赣榆县| 成都市|