主题:碰到点问题,大概和用户控件有关
问题是我在用户控件里点收藏时,收藏物品的主键是空的
在用TABLE控件显示这个用户控件的时候是正常的,但是在这个用DATALIST控件显示这个用户控件的时候就出问题了。
下面是用户控件的代码:
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;
public partial class productInfo : System.Web.UI.UserControl
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
private string productID;
public string ProductID
{
set
{
this.productID = value.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlCommand cmd = new SqlCommand("select * from T_Product_Info where productID='" + productID + "'", con);
con.Open();
SqlDataReader sdr=cmd.ExecuteReader();
sdr.Read();
this.lblProductName.Text=sdr.GetString(1);
this.lblMarketPrice.Text=sdr.GetString(2).ToString();
this.lblOurPrice.Text=sdr.GetString(3).ToString();
this.hLinkDetails.NavigateUrl = "productDetails.aspx?productID=" + this.productID;
this.productImage.ImageUrl=sdr.GetString(5).ToString();
}
}
protected void btnBuy_Click(object sender, EventArgs e)
{
if (Session["userName"] == null)
{
Response.Write("<script>alert('您还没有登录,请登录后再购买,谢谢合作!');location='javascript:history.go(-1)';</script>");
}
else
{
con.Open();
string sqlstrProduct = "select count(*) from T_ShoppingCart_Info where productID='" + productID + "'";
SqlCommand cmdProduct = new SqlCommand(sqlstrProduct, con);
cmdProduct.ExecuteNonQuery();
int count = Convert.ToInt32(cmdProduct.ExecuteScalar());
if (count > 0)
{
string sqlstrNum = "select productNum from T_ShoppingCart_Info where productID='" + productID + "'";
SqlCommand cmdNum = new SqlCommand(sqlstrNum, con);
int num = Convert.ToInt32(cmdNum.ExecuteScalar());
num = num + 1;
string sqlstrAddOne = "update T_ShoppingCart_Info set productNum='" + num + " 'where productID='" + productID + "'";
SqlCommand cmdAddOne = new SqlCommand(sqlstrAddOne, con);
cmdAddOne.ExecuteNonQuery();
}
else
{
string sqlstrShop = "insert into T_ShoppingCart_Info" + "(productID,productName,userName,productNum,ourPrice)"
+ "values('" + productID + "','"+lblProductName.Text+"','" + Session["userName"] + "', 1 ,'"+lblOurPrice.Text+"' )";
SqlCommand cmdShop = new SqlCommand(sqlstrShop, con);
cmdShop.ExecuteNonQuery();
}
con.Close();
Response.Redirect("ShoppingCart.aspx");
}
}
[color=FF0000][color=0000FF][color=008080][color=0000FF][color=FF0000]protected void btnCollect_Click(object sender, EventArgs e)
{
con.Open();
if (Session["userName"] == null)
{
Response.Write("<script>alert('请先登陆,谢谢!');location='javascript:history.go(-1)';</script>");
}
else
{
string sqlstrSelect = "select count(*) from T_MyCollection where userName='" + Session["userName"].ToString() + "' and productID='" + productID + "'";
SqlCommand cmdSelect = new SqlCommand(sqlstrSelect, con);
int count = Convert.ToInt32(cmdSelect.ExecuteScalar());
if (count > 0)
{
Response.Write("<script>alert('该商品已被收藏!');location='javascript:history.go(-1)';</script>");
}
else
{
string sqlstrCollection = "insert into T_MyCollection " + " (userName,productID)" + "values('" + Session["userName"].ToString() + "','" + productID + "')";
SqlCommand cmdCollection = new SqlCommand(sqlstrCollection, con);
cmdCollection.ExecuteNonQuery();
Response.Write("<script>alert('收藏成功!');location='javascript:history.go(-1)';</script>");
}
}
con.Close();
}
}[/color][/color][/color][/color][/color]
下面是页面代码:
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;
public partial class showAllHotProducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = DBCon.createCon();
SqlCommand cmd = new SqlCommand("select * from T_Product_Info where isHot='是' order by productID Asc", con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
this.DataList1.DataSource = sdr;
this.DataList1.DataBind();
sdr.Close();
con.Close();
}
}
}
在用TABLE控件显示这个用户控件的时候是正常的,但是在这个用DATALIST控件显示这个用户控件的时候就出问题了。
下面是用户控件的代码:
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;
public partial class productInfo : System.Web.UI.UserControl
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
private string productID;
public string ProductID
{
set
{
this.productID = value.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlCommand cmd = new SqlCommand("select * from T_Product_Info where productID='" + productID + "'", con);
con.Open();
SqlDataReader sdr=cmd.ExecuteReader();
sdr.Read();
this.lblProductName.Text=sdr.GetString(1);
this.lblMarketPrice.Text=sdr.GetString(2).ToString();
this.lblOurPrice.Text=sdr.GetString(3).ToString();
this.hLinkDetails.NavigateUrl = "productDetails.aspx?productID=" + this.productID;
this.productImage.ImageUrl=sdr.GetString(5).ToString();
}
}
protected void btnBuy_Click(object sender, EventArgs e)
{
if (Session["userName"] == null)
{
Response.Write("<script>alert('您还没有登录,请登录后再购买,谢谢合作!');location='javascript:history.go(-1)';</script>");
}
else
{
con.Open();
string sqlstrProduct = "select count(*) from T_ShoppingCart_Info where productID='" + productID + "'";
SqlCommand cmdProduct = new SqlCommand(sqlstrProduct, con);
cmdProduct.ExecuteNonQuery();
int count = Convert.ToInt32(cmdProduct.ExecuteScalar());
if (count > 0)
{
string sqlstrNum = "select productNum from T_ShoppingCart_Info where productID='" + productID + "'";
SqlCommand cmdNum = new SqlCommand(sqlstrNum, con);
int num = Convert.ToInt32(cmdNum.ExecuteScalar());
num = num + 1;
string sqlstrAddOne = "update T_ShoppingCart_Info set productNum='" + num + " 'where productID='" + productID + "'";
SqlCommand cmdAddOne = new SqlCommand(sqlstrAddOne, con);
cmdAddOne.ExecuteNonQuery();
}
else
{
string sqlstrShop = "insert into T_ShoppingCart_Info" + "(productID,productName,userName,productNum,ourPrice)"
+ "values('" + productID + "','"+lblProductName.Text+"','" + Session["userName"] + "', 1 ,'"+lblOurPrice.Text+"' )";
SqlCommand cmdShop = new SqlCommand(sqlstrShop, con);
cmdShop.ExecuteNonQuery();
}
con.Close();
Response.Redirect("ShoppingCart.aspx");
}
}
[color=FF0000][color=0000FF][color=008080][color=0000FF][color=FF0000]protected void btnCollect_Click(object sender, EventArgs e)
{
con.Open();
if (Session["userName"] == null)
{
Response.Write("<script>alert('请先登陆,谢谢!');location='javascript:history.go(-1)';</script>");
}
else
{
string sqlstrSelect = "select count(*) from T_MyCollection where userName='" + Session["userName"].ToString() + "' and productID='" + productID + "'";
SqlCommand cmdSelect = new SqlCommand(sqlstrSelect, con);
int count = Convert.ToInt32(cmdSelect.ExecuteScalar());
if (count > 0)
{
Response.Write("<script>alert('该商品已被收藏!');location='javascript:history.go(-1)';</script>");
}
else
{
string sqlstrCollection = "insert into T_MyCollection " + " (userName,productID)" + "values('" + Session["userName"].ToString() + "','" + productID + "')";
SqlCommand cmdCollection = new SqlCommand(sqlstrCollection, con);
cmdCollection.ExecuteNonQuery();
Response.Write("<script>alert('收藏成功!');location='javascript:history.go(-1)';</script>");
}
}
con.Close();
}
}[/color][/color][/color][/color][/color]
下面是页面代码:
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;
public partial class showAllHotProducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = DBCon.createCon();
SqlCommand cmd = new SqlCommand("select * from T_Product_Info where isHot='是' order by productID Asc", con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
this.DataList1.DataSource = sdr;
this.DataList1.DataBind();
sdr.Close();
con.Close();
}
}
}