主题:求助。。。。
大家来帮忙看看是怎么回事,谢谢了!
我要用代码实现删除商品种类,在删之前要先确定没有该种类的商品才可以删
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
string sqlstrType = "select * from T_ProductType";
SqlDataAdapter sdaType = new SqlDataAdapter(sqlstrType, con);
DataSet dsType = new DataSet();
sdaType.Fill(dsType);
gvProductType.DataSource = dsType;
gvProductType.DataBind(); con.Close();
}
}
protected void gvProductType_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
con.Open();
int index=e.RowIndex ;
GridViewRow gvr=gvProductType.Rows[index];
string strProductTypeID = gvr.Cells[0].Text;
string sqlstrTestTypeID = "select count(*) from T_Product_Info where productTypeID='" + strProductTypeID + "'";
SqlCommand cmdTestTypeID = new SqlCommand(sqlstrTestTypeID, con);
int count = Convert.ToInt32(cmdTestTypeID.ExecuteScalar());
if (count > 1)
{
Response.Write("<script>alert('还有该种类的商品存在,不能删除!');location='javascript:history.go(-1)';</script>");
}
else
{
string sqlstrDelete = "delete from T_ProductType where productTypeID='" + strProductTypeID + "'";
SqlCommand cmdDelete = new SqlCommand(sqlstrDelete, con);
cmdDelete.ExecuteNonQuery();
Response.Write("<script>alert('删除成功!');location='javascript:history.go(-1)';</script>");
}
con.Close();
Response.Redirect("ManageProductType.aspx");
}
我先用了上面的方法,用Response.Redirect("ManageProductType.aspx");返回该页面来重新绑定。结果可以成功重新绑定删除数据后的GV,问题是Response.Write("<script>alert('还有该种类的商品存在,不能删除!');location='javascript:history.go(-1)';</script>");和Response.Write("<script>alert('删除成功!');location='javascript:history.go(-1)';</script>");没有反映。
然后我又换了一个方法:
第二种方法是建立一个GV绑定类,结果Response.Write("<script>alert('还有该种类的商品存在,不能删除!');location='javascript:history.go(-1)';</script>");和Response.Write("<script>alert('删除成功!');location='javascript:history.go(-1)';</script>");两句可以正常了,但是删除以后GV不能自动绑定删除以后的GV,必须要刷新后才可以。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
BindTogvProductType();
con.Close();
}
}
protected void gvProductType_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
con.Open();
int index=e.RowIndex ;
GridViewRow gvr=gvProductType.Rows[index];
string strProductTypeID = gvr.Cells[0].Text;
string sqlstrTestTypeID = "select count(*) from T_Product_Info where productTypeID='" + strProductTypeID + "'";
SqlCommand cmdTestTypeID = new SqlCommand(sqlstrTestTypeID, con);
int count = Convert.ToInt32(cmdTestTypeID.ExecuteScalar());
if (count > 1)
{
Response.Write("<script>alert('还有该种类的商品存在,不能删除!');location='javascript:history.go(-1)';</script>");
}
else
{
string sqlstrDelete = "delete from T_ProductType where productTypeID='" + strProductTypeID + "'";
SqlCommand cmdDelete = new SqlCommand(sqlstrDelete, con);
cmdDelete.ExecuteNonQuery();
Response.Write("<script>alert('删除成功!');location='javascript:history.go(-1)';</script>");
}
BindTogvProductType();
con.Close();
}
protected void BindTogvProductType()
{
string sqlstrType = "select * from T_ProductType";
SqlDataAdapter sdaType = new SqlDataAdapter(sqlstrType, con);
DataSet dsType = new DataSet();
sdaType.Fill(dsType);
gvProductType.DataSource = dsType;
gvProductType.DataBind();
}
我要用代码实现删除商品种类,在删之前要先确定没有该种类的商品才可以删
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
string sqlstrType = "select * from T_ProductType";
SqlDataAdapter sdaType = new SqlDataAdapter(sqlstrType, con);
DataSet dsType = new DataSet();
sdaType.Fill(dsType);
gvProductType.DataSource = dsType;
gvProductType.DataBind(); con.Close();
}
}
protected void gvProductType_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
con.Open();
int index=e.RowIndex ;
GridViewRow gvr=gvProductType.Rows[index];
string strProductTypeID = gvr.Cells[0].Text;
string sqlstrTestTypeID = "select count(*) from T_Product_Info where productTypeID='" + strProductTypeID + "'";
SqlCommand cmdTestTypeID = new SqlCommand(sqlstrTestTypeID, con);
int count = Convert.ToInt32(cmdTestTypeID.ExecuteScalar());
if (count > 1)
{
Response.Write("<script>alert('还有该种类的商品存在,不能删除!');location='javascript:history.go(-1)';</script>");
}
else
{
string sqlstrDelete = "delete from T_ProductType where productTypeID='" + strProductTypeID + "'";
SqlCommand cmdDelete = new SqlCommand(sqlstrDelete, con);
cmdDelete.ExecuteNonQuery();
Response.Write("<script>alert('删除成功!');location='javascript:history.go(-1)';</script>");
}
con.Close();
Response.Redirect("ManageProductType.aspx");
}
我先用了上面的方法,用Response.Redirect("ManageProductType.aspx");返回该页面来重新绑定。结果可以成功重新绑定删除数据后的GV,问题是Response.Write("<script>alert('还有该种类的商品存在,不能删除!');location='javascript:history.go(-1)';</script>");和Response.Write("<script>alert('删除成功!');location='javascript:history.go(-1)';</script>");没有反映。
然后我又换了一个方法:
第二种方法是建立一个GV绑定类,结果Response.Write("<script>alert('还有该种类的商品存在,不能删除!');location='javascript:history.go(-1)';</script>");和Response.Write("<script>alert('删除成功!');location='javascript:history.go(-1)';</script>");两句可以正常了,但是删除以后GV不能自动绑定删除以后的GV,必须要刷新后才可以。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
BindTogvProductType();
con.Close();
}
}
protected void gvProductType_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
con.Open();
int index=e.RowIndex ;
GridViewRow gvr=gvProductType.Rows[index];
string strProductTypeID = gvr.Cells[0].Text;
string sqlstrTestTypeID = "select count(*) from T_Product_Info where productTypeID='" + strProductTypeID + "'";
SqlCommand cmdTestTypeID = new SqlCommand(sqlstrTestTypeID, con);
int count = Convert.ToInt32(cmdTestTypeID.ExecuteScalar());
if (count > 1)
{
Response.Write("<script>alert('还有该种类的商品存在,不能删除!');location='javascript:history.go(-1)';</script>");
}
else
{
string sqlstrDelete = "delete from T_ProductType where productTypeID='" + strProductTypeID + "'";
SqlCommand cmdDelete = new SqlCommand(sqlstrDelete, con);
cmdDelete.ExecuteNonQuery();
Response.Write("<script>alert('删除成功!');location='javascript:history.go(-1)';</script>");
}
BindTogvProductType();
con.Close();
}
protected void BindTogvProductType()
{
string sqlstrType = "select * from T_ProductType";
SqlDataAdapter sdaType = new SqlDataAdapter(sqlstrType, con);
DataSet dsType = new DataSet();
sdaType.Fill(dsType);
gvProductType.DataSource = dsType;
gvProductType.DataBind();
}