asp.net更新指定記錄的方法
本文實(shí)例講述了asp.net更新指定記錄的方法。分享給大家供大家參考。具體方法如下:
我們先來(lái)看html頁(yè)面:
<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="商品編號(hào)" HeaderText="商品編號(hào)" />
<asp:BoundField DataField="商品名稱" HeaderText="商品名稱" />
<asp:BoundField DataField="商品數(shù)量" HeaderText="商品數(shù)量" />
<asp:BoundField DataField="商品單價(jià)" HeaderText="商品單價(jià)" />
<asp:HyperLinkField DataNavigateUrlFields="商品編號(hào)" DataNavigateUrlFormatString="Default.aspx?商品編號(hào)={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="商品單價(jià):"></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>
由上面頁(yè)面提交過(guò)來(lái)的數(shù)據(jù)我們接受然后利用sql執(zhí)行更新數(shù)據(jù)庫(kù)
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í)行頁(yè)面時(shí)
{
GridViewBind();//綁定自定義方法GridViewBind
if (Request.QueryString["商品編號(hào)"] != null)//判斷,如果可以獲取到id的值,則執(zhí)行以下操作
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_shopping05 where 商品編號(hào)=" + Request.QueryString["商品編號(hào)"] + "", 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["商品單價(jià)"].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 + "',商品單價(jià)='" + this.TxtPrice.Text + "' where 商品編號(hào)=" + Request["商品編號(hào)"], con);
com.ExecuteNonQuery();
GridViewBind();
Response.Write("<script language=javascript>alert('恭喜您!信息更新成功!')</script>");
}
catch
{
Response.Write("<script language=javascript>alert('很遺憾!信息更新失??!')</script>");
}
}
}
原理是這樣的,我們點(diǎn)擊經(jīng)編輯的數(shù)據(jù)時(shí)會(huì)傳一個(gè)ID過(guò)來(lái),然后我們?cè)倮胹ql把接受過(guò)來(lái)的數(shù)據(jù)進(jìn)行update即可了。
希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。
- asp.net中CSharpThinking擴(kuò)展方法分析
- ASP.NET批量下載文件的方法
- ASP.NET私有構(gòu)造函數(shù)用法分析
- 水晶報(bào)表asp.net的webform下基本用法實(shí)例
- asp.net實(shí)現(xiàn)word文檔在線預(yù)覽功能的方法
- asp.net頁(yè)面觸發(fā)事件panel滾動(dòng)條高度不變的實(shí)現(xiàn)方法
- asp.net C#實(shí)現(xiàn)解壓縮文件的方法
- asp.net導(dǎo)出excel數(shù)據(jù)的常見(jiàn)方法匯總
- ASP.NET實(shí)現(xiàn)根據(jù)IP獲取省市地址的方法
- asp.net使用DataGridTree實(shí)現(xiàn)下拉樹(shù)的方法
相關(guān)文章
javascript實(shí)現(xiàn)listbox左右移動(dòng)實(shí)現(xiàn)代碼
javascript實(shí)現(xiàn)listbox左右移動(dòng)實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-05-05asp.net 數(shù)據(jù)類型轉(zhuǎn)換類代碼
asp.net 數(shù)據(jù)類型轉(zhuǎn)換類代碼,需要的朋友可以參考下2012-06-06.Net Core使用Socket與樹(shù)莓派進(jìn)行通信詳解
這篇文章主要為大家詳細(xì)介紹了.Net Core使用Socket與樹(shù)莓派進(jìn)行通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09MVC+EasyUI+三層新聞網(wǎng)站建立 驗(yàn)證碼生成(三)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第三篇,教大家如何生成驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07ASP.NET實(shí)現(xiàn)從服務(wù)器下載文件問(wèn)題處理
本文主要介紹了ASP.NET實(shí)現(xiàn)從服務(wù)器下載文件問(wèn)題處理,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02