濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > asp.net更新指定記錄的方法

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

熱門(mén)標(biāo)簽:電銷語(yǔ)音機(jī)器人型號(hào)參數(shù) 征途美甲店地圖標(biāo)注 太原400電話上門(mén)辦理 昆明語(yǔ)音電銷機(jī)器人價(jià)格 百度地圖怎樣做地圖標(biāo)注 柳州電銷機(jī)器人公司 400電話如何申請(qǐng)取消 騰訊地圖標(biāo)注手機(jī) 浦發(fā)電話機(jī)器人提醒還款

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

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

復(fù)制代碼 代碼如下:
%@ 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="商品編號(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">
                        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="商品單價(jià):">/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>

由上面頁(yè)面提交過(guò)來(lái)的數(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過(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ù)的方法

標(biāo)簽:陽(yáng)泉 德陽(yáng) 蘭州 天門(mén) 張家界 江蘇 新疆 白山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net更新指定記錄的方法》,本文關(guān)鍵詞  asp.net,更新,指定,記錄,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net更新指定記錄的方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于asp.net更新指定記錄的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    舒城县| 肥东县| 锡林郭勒盟| 陈巴尔虎旗| 嘉禾县| 黄石市| 成武县| 五台县| 怀仁县| 临江市| 祁门县| 斗六市| 天台县| 河西区| 都安| 白水县| 上杭县| 波密县| 金华市| 岢岚县| 曲周县| 哈密市| 清新县| 南乐县| 绥宁县| 西畴县| 龙井市| 丰都县| 博白县| 定陶县| 婺源县| 大洼县| 永吉县| 若羌县| 谢通门县| 丹江口市| 葫芦岛市| 璧山县| 云梦县| 镇平县| 大安市|