本文实例讲述了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=”更新指定数据” 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”>

                        &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=”商品数量:”></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>

由上面页面提交过来的数据我们接受然后利用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程序设计有所帮助。

您可能感兴趣的文章:

  • asp.net中CSharpThinking扩展方法分析
  • ASP.NET批量下载文件的方法
  • ASP.NET私有构造函数用法分析
  • 水晶报表asp.net的webform下基本用法实例
  • asp.net实现word文档在线预览功能的方法
  • asp.net页面触发事件panel滚动条高度不变的实现方法
  • asp.net C#实现解压缩文件的方法
  • asp.net导出excel数据的常见方法汇总
  • ASP.NET实现根据IP获取省市地址的方法
  • asp.net使用DataGridTree实现下拉树的方法
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。