本文實例講述了asp.net更新指定記錄的方法。分享給大家供大家參考。具體方法如下:
我們先來看html頁面:
復制代碼 代碼如下:
%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
form id="form1" runat="server">
nbsp;
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="更新指定數(shù)據(jù)" 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="商品編號" HeaderText="商品編號" />
asp:BoundField DataField="商品名稱" HeaderText="商品名稱" />
asp:BoundField DataField="商品數(shù)量" HeaderText="商品數(shù)量" />
asp:BoundField DataField="商品單價" HeaderText="商品單價" />
asp:HyperLinkField DataNavigateUrlFields="商品編號" DataNavigateUrlFormatString="Default.aspx?商品編號={0}"
HeaderText="更新" Text="更新" />
/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" />
HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
AlternatingRowStyle BackColor="White" ForeColor="#284775" />
/asp:GridView>
/td>
/tr>
tr>
td colspan="2" style="width: 496px" align="center">
nbsp;/td>
/tr>
tr>
td colspan="2" style="width: 496px">
asp:Label ID="Label3" runat="server" Font-Size="Smaller" Text="商品名稱:" 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="商品數(shù)量:">/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="商品單價:">/asp:Label>
asp:TextBox ID="TxtPrice" runat="server">/asp:TextBox>/td>
/tr>
tr>
td colspan="2" style="width: 496px">
nbsp;asp:Button ID="BtnUpdate" runat="server" OnClick="BtnUpdate_Click" Text="更新" Width="55px" />/td>
/tr>
/table>
/div>
/form>
由上面頁面提交過來的數(shù)據(jù)我們接受然后利用sql執(zhí)行更新數(shù)據(jù)庫
復制代碼 代碼如下:
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)//首次執(zhí)行頁面時
{
GridViewBind();//綁定自定義方法GridViewBind
if (Request.QueryString["商品編號"] != null)//判斷,如果可以獲取到id的值,則執(zhí)行以下操作
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_shopping05 where 商品編號=" + Request.QueryString["商品編號"] + "", con);
DataSet ds = new DataSet();
ada.Fill(ds, "tb_shopping05");
DataRowView drv = ds.Tables["tb_shopping05"].DefaultView[0];
this.TxtName.Text = drv["商品名稱"].ToString();
this.TxtNum.Text = drv["商品數(shù)量"].ToString();
this.TxtPrice.Text = drv["商品單價"].ToString();
}
}
}
public void GridViewBind()//綁定GridView控件的自定義方法
{
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 商品名稱='" + this.TxtName.Text + "',商品數(shù)量='" + this.TxtNum.Text + "',商品單價='" + this.TxtPrice.Text + "' where 商品編號=" + Request["商品編號"], con);
com.ExecuteNonQuery();
GridViewBind();
Response.Write("script language=javascript>alert('恭喜您!信息更新成功!')/script>");
}
catch
{
Response.Write("script language=javascript>alert('很遺憾!信息更新失?。?)/script>");
}
}
}
原理是這樣的,我們點擊經(jīng)編輯的數(shù)據(jù)時會傳一個ID過來,然后我們再利用sql把接受過來的數(shù)據(jù)進行update即可了。
希望本文所述對大家的asp.net程序設(shè)計有所幫助。
您可能感興趣的文章:- asp.net中CSharpThinking擴展方法分析
- ASP.NET批量下載文件的方法
- ASP.NET私有構(gòu)造函數(shù)用法分析
- 水晶報表asp.net的webform下基本用法實例
- asp.net實現(xiàn)word文檔在線預覽功能的方法
- asp.net頁面觸發(fā)事件panel滾動條高度不變的實現(xiàn)方法
- asp.net C#實現(xiàn)解壓縮文件的方法
- asp.net導出excel數(shù)據(jù)的常見方法匯總
- ASP.NET實現(xiàn)根據(jù)IP獲取省市地址的方法
- asp.net使用DataGridTree實現(xiàn)下拉樹的方法