佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > asp.net更新指定記錄的方法

asp.net更新指定記錄的方法

熱門標簽:電商新玩法 國美全國運營中心 科大訊飛語音識別系統(tǒng) 客戶服務 百度AI接口 網(wǎng)站排名優(yōu)化 電銷業(yè)務 人工智能

本文實例講述了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程序設計有所幫助。

您可能感興趣的文章:
  • asp.net中CSharpThinking擴展方法分析
  • ASP.NET批量下載文件的方法
  • ASP.NET私有構造函數(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)下拉樹的方法

標簽:南平 益陽 棗莊 攀枝花 拉薩 POS機 廈門 咸寧

巨人網(wǎng)絡通訊聲明:本文標題《asp.net更新指定記錄的方法》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    大关县| 大渡口区| 富蕴县| 聊城市| 韩城市| 兴安盟| 旬邑县| 东乡县| 积石山| 八宿县| 天长市| 嘉峪关市| 青冈县| 宾川县| 唐海县| 砀山县| 荥经县| 达日县| 临沂市| 鹰潭市| 亚东县| 松桃| 长子县| 北辰区| 犍为县| 汨罗市| 谷城县| 全椒县| 鱼台县| 大宁县| 元阳县| 马尔康县| 昭觉县| 喀喇沁旗| 大关县| 舒城县| 黔东| 乐清市| 宜昌市| 揭西县| 五指山市|