繁体   English   中英

gridview中的文本框不会自动回发

[英]Text box in gridview not auto post back

我的gridview在回发时缺少先前的数据。 在这种情况下,当我从gvFinalised gridview中检测到不足的库存时,得到类别,然后得到库存最高的产品并显示在gvSuggested gridview中。

但是,在我的gvFinalised ,我们说三种产品中的三种不同类别是不够的,比如说它们是面条,罐头食品和饮料。 它应该在gvSuggested显示三个类别不同的​​产品,每个类别中的库存最高。

但是,我现在的问题是,它只显示最后一项。 例如,在这种情况下,饮料。 饮料之前的数据将全部清除。

这是有关如何检测库存不足的代码:

protected void tbQuantity_TextChanged(object sender, EventArgs e)
{
    tempList = new Dictionary<string, string>();
    distSPUItemList = new Dictionary<string, int>();
    bool valid = true;
    string quantityStr = "", prodID = "";
    int packagesNeeded = 0, totalUnit = 0, quantity = 0;

    //Get the total packages needed for this distribution
    packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

    foreach (GridViewRow gr in gvFinalised.Rows)
    {
        //Clear label error message
        Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
        lblCheckAmount.Text = "";

        //Get the product variant ID which set as DataKeyNames and product quantity from selected row index
        prodID = gvFinalised.DataKeys[gr.RowIndex].Value.ToString();

        var tbQuantity = gr.FindControl("tbQuantity") as TextBox;
        if (tbQuantity != null)
        {
            //Check if the input is numeric
            quantityStr = tbQuantity.Text;
            if (!int.TryParse(quantityStr, out quantity))
            {
                lblCheckAmount.Text = "Non-numeric input!";
                TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox;
            }
            else
            {
                //Add both objects into Dictionary
                tempList.Add(prodID, quantityStr);
            }
        }
    }

    //Portion to check the storage level for each products stored in tempList
    //Loop thru tempList. key as prod variant ID, tempList.Keys as quantity
    foreach (string key in tempList.Keys)
    {
        //Get total unit of each products
        totalUnit = prodPackBLL.getTotalProductUnit(key);
        valid = true;

        //Check if unitQuantity exceed storage level
        if (((Convert.ToInt32(tempList[key])) * packagesNeeded) > totalUnit)
        {
            //Get the label control in gridview
            foreach (GridViewRow gr in gvFinalised.Rows)
            {
                if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString())
                {
                    //Change the color of textBox and display the insufficient message
                    valid = false;
                    //Automatically uncheck the checkBox if invalid
                    TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox;
                    Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
                    lblCheckAmount.Text = "Insufficient stock!";

                    //Here is the place where I collect the data and display in gvSuggested
                    getSuggested(key);
                }
            }
        }
        else
        {
            if (totalUnit - ((Convert.ToInt32(tempList[key])) * packagesNeeded) == 0)
            {

                foreach (GridViewRow gr in gvFinalised.Rows)
                {
                    if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString())
                    {
                        valid = true;
                        Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
                        lblCheckAmount.Attributes["style"] = "color:#ffb848";
                        lblCheckAmount.Text = "Stock becomes 0!";
                    }
                }
            }
        }

        //Portion to check for valid products to be inserted into distSPUItemList
        if (valid)
        {
            //Set the textBox color
            foreach (GridViewRow gr in gvFinalised.Rows)
            {
                if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString())
                {
                    TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox;
                }
            }
            //Validated items store into another list to perform Sql statement when button on click
            distSPUItemList.Add(key, (Convert.ToInt32(tempList[key]) * packagesNeeded));
        }
    }
}

这是用于填充我的gvSuggetedgetSuggested()方法:

protected void getSuggested(string prodVariantID)
{
    string categoryName = prodPackBLL.getCategoryByProdVariantID(prodVariantID);

    //Get the list of substitute product with highest storage level  sorted in descending order
    List<ProductPacking> prodSubstitute = new List<ProductPacking>();
    List<string> lstCategory = new List<string>();
    List<string> prodIDList = new List<string>();
    List<DistributionStandardPackingUnitItems> distSPUItem = new List<DistributionStandardPackingUnitItems>();

    distSPUItem = this.SuggestedItems;

    //Find list of substitute with highest stock level and replace the product
    prodSubstitute = prodPackBLL.getProductIDWithHighestStock(categoryName);

    for (int count = 0; count < prodSubstitute.Count; count++)
    {
        //To prevent duplication of same product and select those catories which are in current category and counting them and taking them if there are less than 1 occurrences
        if (!prodIDList.Contains(prodSubstitute[count].id) && !(lstCategory.Where(x => x.Equals(categoryName)).Select(x => x).Count() >= 2))
        {
            prodIDList.Add(prodSubstitute[count].id);
            lstCategory.Add(categoryName);
        }
    }

    for (int j = 0; j < prodIDList.Count; j++)
    {
        //Get the detail of the product added into prodList and add it into distSPUItem List
        distSPUItem.Add(packBLL.getSPUItemDetailByID(prodIDList[j]));
    }

    gvSuggested.DataSource = distSPUItem;
    gvSuggested.DataBind();

    this.SuggestedItems = distSPUItem;
}

private List<DistributionStandardPackingUnitItems> SuggestedItems
{
    get
    {
        if (ViewState["SuggestedItems"] == null)
        {
            return new List<DistributionStandardPackingUnitItems>();
        }
        else
        {
            return (List<DistributionStandardPackingUnitItems>)ViewState["SuggestedItems"];
        }
    }
    set
    {
        ViewState["SuggestedItems"] = value;
    }
}

回发时,我使用了viewState来存储数据。 但是,它不起作用。 gvSuggested中的这一行:

this.SuggestedItems = distSPUItem; 

导致我在gridView中的文本框无法自动回发。 如果我删除了它,上面的错误再次出现。 有指导吗? 提前致谢。

请启用tbQuantity文本框EnableAutoPostBack = True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM