溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-06-30 15:47:59 來源:億速云 閱讀:174 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“asp.net更新指定記錄的方法是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“asp.net更新指定記錄的方法是什么”吧!

本文實(shí)例講述了asp.net更新指定記錄的方法。分享給大家供大家參考。具體方法如下:

我們先來看html頁(yè)面:

復(fù)制代碼 代碼如下:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 <form id="form1" runat="server">
        &nbsp;
        <div >
            <table >
                <tr>
                    <td colspan="2" >
                        <asp:Label ID="Label2" runat="server" Text="更新指定數(shù)據(jù)" Font-Bold="True" ForeColor="Blue" Width="132px"></asp:Label></td>
                </tr>
                <tr>
                    <td colspan="2" >
        <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"  align="center">
                        &nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2" >
                        <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" >
                        &nbsp;<asp:Button ID="BtnUpdate" runat="server" OnClick="BtnUpdate_Click" Text="更新" Width="55px" /></td>
                </tr>
            </table>
        </div>
    </form>


由上面頁(yè)面提交過來的數(shù)據(jù)我們接受然后利用sql執(zhí)行更新數(shù)據(jù)庫(kù)

復(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í)行頁(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過來,然后我們?cè)倮胹ql把接受過來的數(shù)據(jù)進(jìn)行update即可了。

到此,相信大家對(duì)“asp.net更新指定記錄的方法是什么”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI