温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

asp.net更新指定记录的方法是什么

发布时间:2021-06-30 15:47:59 来源:亿速云 阅读:175 作者:chen 栏目:开发技术

本篇内容主要讲解“asp.net更新指定记录的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“asp.net更新指定记录的方法是什么”吧!

本文实例讲述了asp.net更新指定记录的方法。分享给大家供大家参考。具体方法如下:

我们先来看html页面:

复制代码 代码如下:

<%@ 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="更新指定数据" 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="商品编号" 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"  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="商品数量:"></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" >
                        &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更新指定记录的方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI