asp.net更新指定記錄的方法_實用技巧

來源:互聯網
上載者:User

本文執行個體講述了asp.net更新指定記錄的方法。分享給大家供大家參考。具體方法如下:

我們先來看html頁面:

複製代碼 代碼如下:
<%@ 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="更新指定資料" 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="商品數量" HeaderText="商品數量" />
                <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="商品數量:"></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>

由上面頁面提交過來的資料我們接受然後利用sql執行更新資料庫
複製代碼 代碼如下:
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)//首次執行頁面時
        {
            GridViewBind();//綁定自訂方法GridViewBind
            if (Request.QueryString["商品編號"] != null)//判斷,如果可以擷取到id的值,則執行以下操作
            {
                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["商品數量"].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 + "',商品數量='" + 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>");
        }
    }
}

原理是這樣的,我們點擊經編輯的資料時會傳一個ID過來,然後我們再利用sql把接受過來的資料進行update即可了。

希望本文所述對大家的asp.net程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.