回 帖 发 新 帖 刷新版面

主题:求助。。。。

大家来帮忙看看是怎么回事,谢谢了!

我要用代码实现删除商品种类,在删之前要先确定没有该种类的商品才可以删


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

回复列表 (共3个回复)

沙发

其实我想后面那种方法已经正确了,但是使用了history.go(-1);,所以返回前一页面。极有可能从缓存中读取这一页面,也就是没有删除的列表。
你可以看一下数据库是否已经删除掉了。

板凳

恩,两种方法在数据库中都已经删除了

3 楼

我把history.go(-1)改成了ManageProductType.aspx了

我来回复

您尚未登录,请登录后再回复。点此登录或注册