The example in this article describes how asp.net updates a specified record. Share to everyone for your reference. The specific methods are as follows:
Let's look at the HTML page first:
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<form id= "Form1" runat= "Server" >
<div style= "Text-align:center" >
<table style= "WIDTH:302PX; height:246px; " >
<tr>
<TD colspan= "2" style= "width:496px" >
<asp:label id= "Label2" runat= "Server" text= "Update specified data" font-bold= "True" forecolor= "Blue" width= "132px" ></asp: Label></td>
</tr>
<tr>
<TD colspan= "2" style= "width:496px" >
<asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "False" cellpadding= "4" font-size= "smaller" Forecolor= "#333333" gridlines= "None" >
<Columns>
<asp:boundfield datafield= "Commodity number" headertext= "Item number"/>
<asp:boundfield datafield= "Commodity name" headertext= "Commodity name"/>
<asp:boundfield datafield= "Quantity of goods" headertext= "quantity of goods"/>
<asp:boundfield datafield= "Commodity Unit Price" headertext= "Commodity Unit Price"/>
<asp:hyperlinkfield datanavigateurlfields= "Item number" datanavigateurlformatstring= "default.aspx? Item number ={0}"
headertext= "Update" text= "Update"/>
</Columns>
<footerstyle backcolor= "#5D7B9D" font-bold= "True" forecolor= "white"/>
<rowstyle backcolor= "#F7F6F3" forecolor= "#333333"/>
<editrowstyle backcolor= "#999999"/>
<selectedrowstyle backcolor= "#E2DED6" font-bold= "True" forecolor= "#333333"/>
<pagerstyle backcolor= "#284775" forecolor= "white" horizontalalign= "Center"/>
<alternatingrowstyle backcolor= "White" forecolor= "#284775"/>
</asp:GridView>
</td>
</tr>
<tr>
<TD colspan= "2" style= "width:496px" align= "Center" >
</td>
</tr>
<tr>
<TD colspan= "2" style= "width:496px" >
<asp:label id= "Label3" runat= "font-size=" smaller "text=" Product Name: "Width=" 65px "></asp:label><asp : TextBox id= "txtname" runat= "Server" ></asp:TextBox>
</td>
</tr>
<tr>
<TD colspan= "2" >
<asp:label id= "Label4" runat= "server" font-size= "smaller" text= "Number of items:" ></asp:Label>
<asp:textbox id= "Txtnum" runat= "Server" ></asp:TextBox></td>
</tr>
<tr>
<TD colspan= "2" >
<asp:label id= "Label5" runat= "server" font-size= "smaller" text= "Product Price:" ></asp:Label>
<asp:textbox id= "Txtprice" runat= "Server" ></asp:TextBox></td>
</tr>
<tr>
<TD colspan= "2" style= "width:496px" >
<asp:button id= "btnupdate" runat= "Server" onclick= "Btnupdate_click" text= "Update" width= "55px"/></td>
</tr>
</table>
</div>
</form>
Data submitted by the above page we accept and then use SQL to perform the update database
Copy Code code as follows:
View Code
Using System;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using System.Data.SqlClient;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! Page.IsPostBack)//When the page is first executed
{
Gridviewbind ();//Binding custom method Gridviewbind
if (request.querystring["item number"]!= null)//judge, if you can get the value of the ID, do the following
{
SqlConnection con = new SqlConnection (configurationsettings.appsettings["Strcon"));
Con. Open ();
SqlDataAdapter ada = new SqlDataAdapter ("select * from tb_shopping05 where commodity number =" + request.querystring["commodity number"] + "", con) ;
DataSet ds = new DataSet ();
Ada. Fill (ds, "tb_shopping05");
DataRowView DRV = ds. tables["Tb_shopping05"]. DEFAULTVIEW[0];
This. txtName.Text = drv["commodity name". ToString ();
This. Txtnum.text = drv["Quantity of goods"]. ToString ();
This. Txtprice.text = drv["commodity Unit Price"]. ToString ();
}
}
}
Custom method for the public void Gridviewbind ()//binding GridView Control
{
SqlConnection con = new SqlConnection (configurationsettings.appsettings["Strcon"));
Con. Open ();
SqlDataAdapter ada = new SqlDataAdapter ("select * from tb_shopping05", con);
DataSet ds = new DataSet ();
Ada. Fill (DS);
Gridview1.datasource = ds;
Gridview1.databind ();
Con. Close ();
}
protected void Btnupdate_click (object sender, EventArgs e)
{
Try
{
SqlConnection con = new SqlConnection (configurationsettings.appsettings["Strcon"));
Con. Open ();
SqlCommand com = new SqlCommand ("Update tb_shopping05 set commodity name = '" + this. txtName.Text + "', number of items = '" + this. Txtnum.text + "', commodity price = '" + this. Txtprice.text + "' where commodity number =" + request["item number"], con);
Com. ExecuteNonQuery ();
Gridviewbind ();
Response.Write ("<script Language=javascript>alert") (' Congratulations! Information Update Successful! ') </script> ");
}
Catch
{
Response.Write ("<script Language=javascript>alert") (' Sorry! Information update failed! ') </script> ");
}
}
}
The principle is that we click on the edited data to pass an ID, and then we use SQL to update the accepted data.
I hope this article will help you with the ASP.net program design.