程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net更新指定記錄的方法

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

編輯:ASP.NET基礎

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved