设置引用int有问题

时间:2014-07-01 18:47:57

标签: c# checkbox ref

我有一个页面,其中包含一个表格中的产品列表(使用div构建但我离题了),一列名为“添加到购物清单”并包含一个复选框。如果选中该复选框,则该行的产品应放入用户的订单中。提交后,用户将被带到订单审核页面,其中包含他们选择的所有项目。

现在的问题是,即使我在选中复选框时将产品数量设置为“1”,每次提交表单时,选择订购的项目数始终为0.

用于查看在提交时是否已选择任何项目进行排序的功能称为checkQtys:

protected bool checkQtys(ref int ItemCnt, ref ArrayList LinesToOrder)
{
    Repeater locationRepeater = (Repeater)FindControl("locationRepeater");

    bool validQtys = true;
    string productID = "";
    int qty;
    qtyErrorMsg.Text = "";
    qtyErrorMsgTop.Text = "";
    foreach (RepeaterItem repItem in locationRepeater.Items)
    {
        if (repItem != null)
        {
            Repeater areaRepeater = (Repeater)repItem.FindControl("areaRepeater");
            if (areaRepeater != null)
            {
                foreach (RepeaterItem skuItm in areaRepeater.Items)
                {

                    if (skuItm != null)
                    {
                        Label SkuID = (Label)skuItm.FindControl("SkuID");
                        Label qtyID = (Label)skuItm.FindControl("qtyID");

                        PlaceHolder inner = (PlaceHolder)skuItm.FindControl("ProductTable");

                        if (inner != null)
                        {
                            foreach (Control ct in inner.Controls)
                            {
                                if (ct is CheckBox)
                                {
                                    CheckBox lineQty = (CheckBox)ct;
                                    Label prodID = (Label)inner.FindControl("productID");
                                    if (lineQty.Checked)
                                    {

                                        productID = prodID.Text;
                                        qty = 1;
                                        LinesToOrder.Add(new LineItem(productID, qty));
                                        ItemCnt++;
                                        validQtys = true;
                                    }

                                }
                            }

                        }
                    }

                }
            }

        }
    }
    return validQtys;
}

*注意:此原件用于检查文本框值 - “添加到购物清单”列最初设置为获取特定数量的用户输入,但已更改为复选框。

这是提交功能:

protected void orderSubmit(object sender, EventArgs e)
{
    int ItemCnt = 0;
    bool validQtys = true;
    ArrayList LinesToOrder = new ArrayList();
    Label lb = FindControl("order") as Label;

    if (checkQtys(ref ItemCnt, ref LinesToOrder))
    {
        string value = checkQtys(ref ItemCnt, ref LinesToOrder).ToString();
        Response.Write("value: " + value + "<br />");
        if (ItemCnt == 0)
        {//make sure at least one item with proper qty amount is entered before submitting the order
            validQtys = false;
            noItemMsg.Visible = true;
            noItemMsg.Text = "<br /><br />You must order at least one item<br />";
            noItemMsgTop.Visible = true;
            noItemMsgTop.Text = "You must order at least one item<br />";
        }
        if (validQtys)
        {//save the information to a session variable and send users to order review page      
            try
            {
                foreach (LineItem WorkLine in LinesToOrder)
                {
                    lb.Text += WorkLine.SKUID + ", " + WorkLine.Qty + ";";
                }

                Session["orderComplete"] = lb.Text;
            }

            catch (Exception x)
            {
                Response.Write(x.Message.ToString());
            }
            Response.Redirect("/united-states/market/office-buildings/obproductmap/OrderReview");

        }
    }
}

上述功能中发生的事情是ItemCnt始终为0,因此用户永远不能进入订单查看页面,即使他们已经检查了产品列表表中的至少一个复选框。我不知道为什么会发生这种情况 - 在checkQtys中设置了ItemCnt的ref int值,它不应该在orderSubmit中作为0进入,除非它实际上是0。

我错过了什么?

0 个答案:

没有答案