asp.net更新指定記錄的方法
更新時間:2014年11月08日 11:14:48 投稿:shichen2014
這篇文章主要介紹了asp.net更新指定記錄的方法,涉及針對指定ID調(diào)用update語句的方法,是非常實用的技巧,需要的朋友可以參考下
本文實例講述了asp.net更新指定記錄的方法。分享給大家供大家參考。具體方法如下:
我們先來看html頁面:
復(fù)制代碼 代碼如下:
<%@ 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="更新指定數(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">
</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">
<asp:Button ID="BtnUpdate" runat="server" OnClick="BtnUpdate_Click" Text="更新" Width="55px" /></td>
</tr>
</table>
</div>
</form>
<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="更新指定數(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">
</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">
<asp:Button ID="BtnUpdate" runat="server" OnClick="BtnUpdate_Click" Text="更新" Width="55px" /></td>
</tr>
</table>
</div>
</form>
由上面頁面提交過來的數(shù)據(jù)我們接受然后利用sql執(zhí)行更新數(shù)據(jù)庫
復(fù)制代碼 代碼如下:
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>");
}
}
}
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文檔在線預(yù)覽功能的方法
- asp.net頁面觸發(fā)事件panel滾動條高度不變的實現(xiàn)方法
- asp.net C#實現(xiàn)解壓縮文件的方法
- asp.net導(dǎo)出excel數(shù)據(jù)的常見方法匯總
- ASP.NET實現(xiàn)根據(jù)IP獲取省市地址的方法
- asp.net使用DataGridTree實現(xiàn)下拉樹的方法
相關(guān)文章
asp.net中通過DropDownList的值去控制TextBox是否可編寫的實現(xiàn)代碼
Web窗體上有兩控件,DropDownList1,TextBox1,當DropDownList的值選擇是YES的時候,TextBox1可編輯,當選擇NO的時候,TextBox1的值為空,并且不能編輯,該如何實現(xiàn)2012-11-11
asp.net實現(xiàn)DataList與Repeater嵌套綁定的方法
這篇文章主要介紹了asp.net實現(xiàn)DataList與Repeater嵌套綁定的方法,結(jié)合實例形式分析了DataList與Repeater的步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-04-04
ASP.NET MVC從控制器傳遞數(shù)據(jù)到視圖的四種方式詳解
本篇文章主要介紹了ASP.NET MVC從控制器傳遞數(shù)據(jù)到視圖的四種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01
Asp.net core實現(xiàn)PushStream視頻流推送
這篇文章介紹了Asp.net core實現(xiàn)PushStream視頻流推送的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
asp.net AJAX實現(xiàn)無刷新獲得數(shù)據(jù)
提供一個使用AJAX實現(xiàn)無刷新判斷注冊用戶名是否被注冊的代碼:2008-11-11
.NET?Core?Web?APi類庫內(nèi)嵌運行的方法
這篇文章主要介紹了.NET?Core?Web?APi類庫內(nèi)嵌運行的方法,本節(jié)我們重點討論如何內(nèi)嵌運行.NET Core Web APi類庫,同時介紹了兩種激活比如控制器特性方案,需要的朋友可以參考下2022-09-09

