Update all records of the gridview at one time in Asp.net 2.0

Source: Internet
Author: User
Asp.net 2.0 updates all the records of the gridview at one time in Asp.net 2.0. The gridview control is a very good control. Sometimes, a row in a gridview control may be a text box. How can I update all modified records at a time? There are two ways to update all records. One is to use sqldatasource to update all records, but this method is slow, because every time a record is updated, a data connection is established and the updatecommand is executed, which will affect the performance, but let's first look at the implementation method:

<% @ Page Language = "C #" %>

<SCRIPT runat = "server">

Void button#click (Object sender, eventargs E)
{
For (INT I = 0; I <gridview1.rows. Count; I ++)
{
Gridviewrow ROW = gridview1.rows;
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. 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" performanceid = "sqlperformance1" 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 [MERs] 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"/>

</Div>
</Form>
</Body>
</Html>


Another method is to use SQL statements, which is fast and easy to understand. Let's look at the method below.

<% @ Page Language = "C #" %>
<% @ Import namespace = "system. Text" %>
<% @ Import namespace = "system. Data. sqlclient" %>
<SCRIPT runat = "server">

Void button#click (Object sender, eventargs E)
{
Stringbuilder query = new stringbuilder ();

For (INT I = 0; I <gridview1.rows. Count; I ++)
{
Gridviewrow ROW = gridview1.rows;
String value1 = (textbox) Row. cells [0]. findcontrol ("textbox2"). Text. Replace ("'","''");
String value2 = (textbox) Row. cells [1]. findcontrol ("textbox3"). Text. Replace ("'","''");
String value3 = gridview1.datakeys. value. tostring ();

Query. append ("Update [MERs] Set [companyName] = '")
. Append (value1). append ("', [contacttitle] = '")
. Append (value2). append ("'where [mermerid] = '")
. 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"/>

</Div>
</Form>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.