首页 新闻 会员 周边

购物中数量加减如何控制在正整数范围内

0
悬赏园豆:40 [待解决问题]

我用datalist做购物车,在购物车中用用两个button按钮来控制textbox中的数量加减,如何实现数量大于0,大神帮帮忙,急用

杨杨昵的主页 杨杨昵 | 初学一级 | 园豆:93
提问于:2015-12-02 20:09
< >
分享
所有回答(2)
0

点击减少按钮时,获取一次值进行判断,如果等于0值不变,大于零就减1

车窗外徘徊 | 园豆:202 (菜鸟二级) | 2015-12-02 21:59

能给代码提示一下吗?我虽然也知道思路,但是代码一直写不对

支持(0) 反对(0) 杨杨昵 | 园豆:93 (初学一级) | 2015-12-02 22:18

@杨杨昵: 把你的代码贴出来我看看哪里不对

支持(0) 反对(0) 车窗外徘徊 | 园豆:202 (菜鸟二级) | 2015-12-02 22:52

@车窗外徘徊: 

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,就不行了,麻烦帮我看看,谢谢了

支持(0) 反对(0) 杨杨昵 | 园豆:93 (初学一级) | 2015-12-03 11:18
0

当点击的时候,获取textbox的值,判断如果小于等于0的时候,按钮不能点击或者点击还是0即可

Kencery | 园豆:357 (菜鸟二级) | 2015-12-03 09:28

代码怎么写啊?我在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();

}
}

减数量部分代码是这样的,帮我看看哪里有问题,谢谢了

支持(0) 反对(0) 杨杨昵 | 园豆:93 (初学一级) | 2015-12-03 13:24

@杨杨昵: 这玩意看代码没用,思想就那样,

  1.前端得到值判断~~

  2.后端得到值在进行判断以下~~~

支持(0) 反对(0) Kencery | 园豆:357 (菜鸟二级) | 2015-12-04 09:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册