ASP.net公司网站 2008-07-18 16:10

字号:    

这个学期ASP 最后一次作业是做一个公司网站,用了一个星期的时间,真的是“呕心沥血”呀


一共有六个功能模块

1、公司简介

2、商城

3、新闻发布

4、留言板

5、管理员管理

6、用户管理


主要是商城这一块,有三种商品的查询方法。

一种是用DropDownlist 控制GridView

一种是用模糊查找

代码如下:

        SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("App_Data/Data.mdf") + ";Integrated Security=True;User Instance=True";//连接数据库
                conn.Open();
            string strSQL = " SELECT [pid], [fenlei], [brand], [pname], [price], [image], [shu] FROM [products] where brand like '%" + Session["guan"].ToString () + "%'";//模糊查询的代码
            SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);//建立数据适配器da
            DataSet ds = new DataSet();
            da.Fill(ds, "products");
            DataTable dt = ds.Tables["products"];

            GridView1.DataSource = dt.DefaultView;
            GridView1.DataBind();//连接GridView


 第三种是前面两种的集合

代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (TextBox1.Text != null)
        {
            Session["dr"] = DropDownList1.SelectedValue;
            Session["wb"] = TextBox1.Text.Trim();
        }
        Label1.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text  != "")
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("App_Data/Data.mdf") + ";Integrated Security=True;User Instance=True";
            conn.Open();
            string strSQL = " SELECT [pid], [fenlei], [brand], [pname], [price], [image], [shu] FROM [products] where pname like '%" + Session["wb"].ToString() + "%' and fenlei='" + Session["dr"] + "'";
            SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);//建立数据适配器da
            DataSet ds = new DataSet();
            da.Fill(ds, "products");
            DataTable dt = ds.Tables["products"];

            GridView1.DataSource = dt.DefaultView;
            GridView1.DataBind();
            Label1.Visible = true;
        }
        else {
            Response.Write("<script>alert('请输入产品名的关键字!');window.window.location.href='gaojisearch.aspx';</script>");

        }
       
        商品订购功能实现:

protected void Page_Load(object sender, EventArgs e)
    {


        if (Session["us"] == null)
        {
            LinkButton1.Visible = false;
            LinkButton2.Visible = false;

        }
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("App_Data/Data.mdf") + ";Integrated Security=True;User Instance=True";
        conn.Open();
        string strSQL = " select sum(price*shu) As totle,sum(shu) AS shu from shopcar where   us='" + Session["us"] + "'";
        SqlCommand comm = new SqlCommand(strSQL, conn);
        SqlDataReader myreader = comm.ExecuteReader();
        myreader.Read();
        TextBox1.Text = myreader["totle"].ToString();//计算总价格
        TextBox2.Text = myreader["shu"].ToString();//计算订购的总数量
        strSQL = "select * from [products] ";
        myreader.Close();
        SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);//建立数据适配器da
        DataSet ds = new DataSet();//
        da.Fill(ds, "products");
        DataTable dt = ds.Tables["products"];

        if (dt.Rows.Count > 0)
        {
            DataRow row = dt.Rows[0];
            Label4.Text = dt.Rows.Count.ToString();

        }
        else
        {
            Label4.Text = "0";
        }
        conn.Close();


    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("xiugai.aspx");//跳转到注册.aspx
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect("shopcar.aspx");
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("App_Data/Data.mdf") + ";Integrated Security=True;User Instance=True";
        conn.Open();
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = GridView1.Rows[index];
        string strSQL = " select sum(price*shu) As totle,sum(shu) AS shu from shopcar where us='" + Session["us"] + "'";
        SqlCommand comm = new SqlCommand(strSQL, conn);
        SqlDataReader myreader = comm.ExecuteReader();
        myreader.Read();
        TextBox1.Text = myreader["totle"].ToString();
        TextBox2.Text = myreader["shu"].ToString();
        myreader.Close();
        strSQL = "select shu from shopcar  where us='" + Session["us"] + "'and pid='" + selectedRow.Cells[0].Text + "'and pname='" + selectedRow.Cells[3].Text + "'and price='" + selectedRow.Cells[4].Text + "'";
        SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);//建立数据适配器da
        DataSet ds = new DataSet();
        da.Fill(ds, "shopcar");
        DataTable dt = ds.Tables["shopcar"];

        if (dt.Rows.Count == 1)
        {
            DataRow row = dt.Rows[0];
            int bv;
            string shu = dt.Rows[0][0].ToString();
            bv = Convert.ToInt32(shu);
            bv = bv + 1;
            strSQL = "update shopcar set shu='" + bv + "'where us='" + Session["us"] + "'and pid='" + selectedRow.Cells[0].Text + "'and fenlei='" + selectedRow.Cells[1].Text + "'and brand='" + selectedRow.Cells[2].Text + "'and pname='" + selectedRow.Cells[3].Text + "'and price='" + selectedRow.Cells[4].Text + "'";
            comm = new SqlCommand(strSQL, conn);
            comm.ExecuteNonQuery();
            strSQL = " select sum(price*shu) As totle,sum(shu) AS shu from shopcar where us='" + Session["us"] + "'";
            comm = new SqlCommand(strSQL, conn);
            myreader = comm.ExecuteReader();
            myreader.Read();
            TextBox1.Text = myreader["totle"].ToString();
            TextBox2.Text = myreader["shu"].ToString();
            myreader.Close();
            Response.Write("<script>alert('恭喜您,您已经成功地把这个物品添加到购物车中,您可以查看购物车中的详细信息!');window.window.location.href='products.aspx';</script>");

        }
        else if (e.CommandName == "Order" && Session["us"] != null)
        {

            strSQL = "insert into shopcar  values('" + Session["us"] + "','" + selectedRow.Cells[0].Text + "','" + selectedRow.Cells[1].Text + "','" + selectedRow.Cells[2].Text + "','" + selectedRow.Cells[3].Text + "','" + selectedRow.Cells[4].Text + "','" + selectedRow.Cells[6].Text + "','" + Session.SessionID + "')";
            comm = new SqlCommand(strSQL, conn);
            comm.ExecuteNonQuery();
            strSQL = " select sum(price*shu) As totle,sum(shu) AS shu from shopcar where us='" + Session["us"] + "'";
            comm = new SqlCommand(strSQL, conn);
            myreader = comm.ExecuteReader();
            myreader.Read();
            TextBox1.Text = myreader["totle"].ToString();
            TextBox2.Text = myreader["shu"].ToString();
            myreader.Close();
            Response.Write("<script>alert('恭喜您,您已经成功地把这个物品添加到购物车中,您可以查看购物车中的详细信息!');window.window.location.href='products.aspx';</script>");
        }

        else
        {
            Response.Write("<script>alert('请先登录或者注册');window.window.location.href='login.aspx';</script>");

        }
        conn.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox3.Text == "")
        {
            Response.Write("<script>alert('输入内容不能为空!');window.window.location.href='products.aspx';</script>");

        }
        if (TextBox3 .Text !="")
        {

            Session["guan"] = TextBox3.Text;
            Response.Redirect("mohusearch.aspx");
        }


    }

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
网易公司版权所有 ©1997-2009