asp.net更新指定记录的方法(2)

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>");
        }
    }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzzpz.html