⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 product_modify.aspx.cs

📁 基于VS05和SQL05做的一个电子商务网站
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.IO;
public partial class product_modify : System.Web.UI.Page
{
    public int payment;
    public string Payment;
    public int isubb = 1;
    public int isscript = 0;
    public int isflash = 1;
    public int isimg = 1;

    public SqlDataReader dr;
    public string strID;
    public string Store;//所属商店
    public string Name;//商品名
    public string Type;//商品类型
    public string Price;//商品单价
    public string Quantity;//库存量
    public string Post;//平邮邮费
    public string Fax;//快递邮费
    public string Picture;//商品图片
    public string Detail;//商品详细
    public string State;//商品状态
    public int state;
    public string Repair;//有无保修
    public int repair;
    public string Invoice;
    public int invoice;
    public string back;//返回
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["msg"] == null)
        {
            back = "";
        }
        else
        {
            back = "<a href='";
            back += Server.UrlDecode(Request.QueryString["msg"].ToString());
            back += "'>返回</a>";
        }

        if (!IsPostBack)
        {
            if (Session["username"] == null)
            {
                Response.Write("<script language='JavaScript'>");
                Response.Write("alert('请先登录!')");
                Response.Write("</script>");
                Response.End();
                return;
            }



            if (Request.QueryString["id"] != null)
            {
                SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
                string id = Request.QueryString["id"].ToString();
                String sql = "SELECT * FROM Products where ProductId=" + id;
                SqlCommand command = new SqlCommand(sql, connection);
                connection.Open();
                dr = command.ExecuteReader();

                if (dr.Read())
                {
                    strID = dr["ProductId"].ToString();
                    Store = dr["UserName"].ToString();
                    Name = dr["ProductName"].ToString();
                    Type = dr["ProductType"].ToString();
                    Price = dr["ProductPrice"].ToString();
                    Quantity = dr["ProductQuantity"].ToString();
                    Post = dr["ProductPost"].ToString();
                    Fax = dr["ProductFax"].ToString();
                    Picture = dr["ProductPicture"].ToString();
                    Detail = dr["ProductDetail"].ToString();
                    State = dr["ProductState"].ToString();
                    state = Int32.Parse(State);
                    Repair = dr["ProductRepair"].ToString();
                    repair = Int32.Parse(Repair);
                    Invoice = dr["ProductInvoice"].ToString();
                    invoice = Int32.Parse(Invoice);
                    Payment = dr["ProductPayment"].ToString();
                    payment = Int32.Parse(Payment);
                }

                myimg.Src = "showProductPicture.aspx?id=" + strID;


                Label1.Text = Store + " 的店铺";
                Label2.Text = Name;
                TextBox2.Text = Detail;
                TextBox3.Text = Price;
                TextBox4.Text = Quantity;
                TextBox5.Text = Post;
                TextBox6.Text = Fax;
                /*判断商品状态*/
                if (state == 1)/*已上架*/
                { RadioButton18.Checked = true; }
                else/*库存*/
                { RadioButton17.Checked = true; }

                /*判断保修情况*/
                if (repair == 1)
                { RadioButton16.Checked = true; }
                else
                { RadioButton15.Checked = true; }

                /*判断发票情况*/
                if (invoice == 1)
                { RadioButton14.Checked = true; }
                else
                { RadioButton13.Checked = true; }

                /*判断付款方式*/
                if (payment == 0)
                { RadioButton12.Checked = true; }
                else if (payment == 1)
                { RadioButton11.Checked = true; }
                else
                { RadioButton10.Checked = true; }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            string price = TextBox3.Text;
            //验证
            //Response.Write("<script language=javascript>window.alert('请输入数字价格!')</script>");
            string quantity = TextBox4.Text;
            string post = TextBox5.Text;
            string fax = TextBox6.Text;
            string detail = TextBox2.Text;
            //detail=TextBox2.Text.Replace("'","''");
            //detail =texttohtml(detail);
            //detail=changechr(detail);
            int payment;
            int repair;
            int invoice;
            int state;

            string imgPath = uploadFile.PostedFile.FileName;
            System.Drawing.Image imgupload = null;
            try
            {
                imgupload = System.Drawing.Image.FromFile(imgPath);
            }
            catch
            {
                //Response.Write("请注意:因为系统桌面上以及我的文档里面的文件具有特殊权限,所以在选择图片时请选择其他地方的图片。");
            }

            if (RadioButton12.Checked)
            { payment = 0; }
            else if (RadioButton11.Checked)
            { payment = 1; }
            else
            { payment = 2; }

            if (RadioButton16.Checked)
            { repair = 1; }
            else
            { repair = 0; }

            if (RadioButton14.Checked)
            { invoice = 1; }
            else
            { invoice = 0; }

            if (RadioButton18.Checked)/*已上架*/
            { state = 1; }
            else/*库存*/
            { state = 0; }

            if (uploadFile.PostedFile.FileName != "" && (Path.GetExtension(uploadFile.PostedFile.FileName) != ".gif" && Path.GetExtension(uploadFile.PostedFile.FileName) != ".jpg"))
            {
                Response.Write("<Script>alert('上传的图片格式必须为gif或jpg!!')</Script>");
            }/*
                else if(uploadFile.PostedFile.FileName!="" && imgupload.Height>200 && imgupload.Width>200)
                {
                    Response.Write("<script>alert('上传的图片的高不的超过200 宽不的超过200!!')</script>");
                }*/
            else
            {

                SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);//调用Web.config连接数据库
                string id = Request.QueryString["id"].ToString();
                String sql = "UPDATE Products set ProductPrice='" + price + "',ProductQuantity='"
                    + quantity + "',ProductPost='" + post + "',ProductFax='" + fax + "',ProductDetail='" + detail +
                    "',ProductState=" + state + ",ProductRepair=" + repair + ",ProductInvoice=" + invoice + ",ProductPayment=" + payment + " where ProductId=" + id;
                SqlCommand command = new SqlCommand(sql, connection);//构件执行数据命令
                connection.Open();//打开数据库
                command.ExecuteReader();//保存从数据库里面返回的一条记录

                if (uploadFile.PostedFile.FileName != "")
                {
                    //----------- update图片
                    Stream imagedatastream;
                    SqlConnection myConn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
                    imagedatastream = Request.Files["uploadFile"].InputStream;
                    int imagedatalen = Request.Files["uploadFile"].ContentLength;
                    string imagedatatype = Request.Files["uploadFile"].ContentType;

                    byte[] image = new byte[imagedatalen];
                    imagedatastream.Read(image, 0, imagedatalen);

                    //String sql="insert into image(image) values(@imgdata)";
                    String Psql = "update [Products] set [ProductPicture]=@imgdata where ProductId=" + Request.QueryString["id"].ToString();

                    SqlCommand Pcommand = new SqlCommand(Psql, myConn);

                    SqlParameter imgdata = new SqlParameter("@imgdata", SqlDbType.Image);
                    imgdata.Value = image;
                    Pcommand.Parameters.Add(imgdata);

                    myConn.Open();
                    Pcommand.ExecuteReader();
                    myConn.Close();
                }
                //-----------

                Response.Write("<script language=javascript>window.alert('保存成功!')</script>");
                //Response.Redirect("DeleteProduct.aspx");
            }

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -