簡體   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