我用datalist做购物车,在购物车中用用两个button按钮来控制textbox中的数量加减,如何实现数量大于0,大神帮帮忙,急用
点击减少按钮时,获取一次值进行判断,如果等于0值不变,大于零就减1
能给代码提示一下吗?我虽然也知道思路,但是代码一直写不对
@杨杨昵: 把你的代码贴出来我看看哪里不对
@车窗外徘徊:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Length > 0 && e.CommandName == "reduce" )
{
int index = e.Item.ItemIndex;
TextBox textbox3 = (TextBox)DataList1.Items[0].FindControl("textbox3");
TextBox textbox4 = (TextBox)DataList1.Items[0].FindControl("textbox4");
Button button6 = (Button)DataList1.Items[0].FindControl("button6");
Connstrclass connstrclass = new Connstrclass();
SqlConnection conn = connstrclass.GetConnection();
string sql1 = "select Cart_Num=@p2 from tb_ShoppingCart where Goods_Id=@p1";
SqlCommand cmd1 = new SqlCommand(sql1, conn);
SqlParameter[] paras1 ={
new SqlParameter("@p2",textbox3 .Text ),
new SqlParameter("@p1",DataList1.DataKeys[e.Item .ItemIndex ].ToString ()),
};
conn.Open();
int result1 = ConnDBHelper.ExecuteCommand(sql1, paras1);
if (textbox3.Text.Trim () == "1")
{
button6.Enabled = false;
DataList1.DataBind();
}
else
{
string sql = "update tb_ShoppingCart set Cart_Num=Cart_Num-1 from tb_ShoppingCart where Goods_Id=@p1";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] paras ={ new SqlParameter("@p1",DataList1.DataKeys[e.Item .ItemIndex ].ToString ()),
};
int result = ConnDBHelper.ExecuteCommand(sql, paras);
conn.Close();
DataList1.DataBind();
}
}
这部分是减数量的代码,有个问题是如果购物车中有两件商品,只能减第一件,点击第二件商品的减数量button,就不行了,麻烦帮我看看,谢谢了
当点击的时候,获取textbox的值,判断如果小于等于0的时候,按钮不能点击或者点击还是0即可
代码怎么写啊?我在datalist中做数量加减,需要控制每一行都能加减,我写了代码只有第一行能加减而且控制在了1以上,其他的不行,怎么办啊?
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Length > 0 && e.CommandName == "reduce" )
{
int index = e.Item.ItemIndex;
TextBox textbox3 = (TextBox)DataList1.Items[0].FindControl("textbox3");
TextBox textbox4 = (TextBox)DataList1.Items[0].FindControl("textbox4");
Button button6 = (Button)DataList1.Items[0].FindControl("button6");
Connstrclass connstrclass = new Connstrclass();
SqlConnection conn = connstrclass.GetConnection();
string sql1 = "select Cart_Num=@p2 from tb_ShoppingCart where Goods_Id=@p1";
SqlCommand cmd1 = new SqlCommand(sql1, conn);
SqlParameter[] paras1 ={
new SqlParameter("@p2",textbox3 .Text ),
new SqlParameter("@p1",DataList1.DataKeys[e.Item .ItemIndex ].ToString ()),
};
conn.Open();
int result1 = ConnDBHelper.ExecuteCommand(sql1, paras1);
if (textbox3.Text.Trim () == "1")
{
button6.Enabled = false;
DataList1.DataBind();
}
else
{
string sql = "update tb_ShoppingCart set Cart_Num=Cart_Num-1 from tb_ShoppingCart where Goods_Id=@p1";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] paras ={ new SqlParameter("@p1",DataList1.DataKeys[e.Item .ItemIndex ].ToString ()),
};
int result = ConnDBHelper.ExecuteCommand(sql, paras);
conn.Close();
DataList1.DataBind();
}
}
减数量部分代码是这样的,帮我看看哪里有问题,谢谢了
@杨杨昵: 这玩意看代码没用,思想就那样,
1.前端得到值判断~~
2.后端得到值在进行判断以下~~~