Record of all GridView updates in asp.net 2.0

Source: Internet
Author: User
Tags bind count tostring xmlns connectionstrings
asp.net in asp.net 2.0, the GridView control is a very good control. Sometimes, a GridView control might
Lines are text boxes, how do you update all the modified records at once? There are two methods, one is to use SqlDataSource to update
All records, but this method is slow, because each update of a record to establish a data connection and perform UpdateCommand, can affect performance,
But first let's look at the implementation method:

<%@ 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>





<title>untitled page</title>


<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] = @c Ustomerid "

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>


Another method is to use the combination of SQL statements, faster, the principle is easy to understand

<%@ 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 ("'; \ \");



}



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>





<title>untitled page</title>


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






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.