GridView在回发之间丢失数据

时间:2014-02-04 11:56:37

标签: c# asp.net gridview viewstate

所以基本上我正在做一个管理包装项目系统。场景是,当我从gvSPU中选择时,gvFinalised应该在数据库中显示该项。之后,当我将新项目添加到gvFinalised时,它应该继续堆叠项目,而不是擦除先前的记录并显示添加的最新记录。这是代码:

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

protected void gvSPU_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

    int rowNo = int.Parse(e.CommandArgument.ToString());
    SPUname = this.gvSPU.DataKeys[rowNo].Value.ToString();
    lblSPUname.Text = SPUname;

    List<DistributionStandardPackingUnitItems> templist = new List<DistributionStandardPackingUnitItems>();
    templist = tempDistSPUI;

    templist = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);
    gvFinalised.DataSource = templist;
    gvFinalised.DataBind();

    this.tempDistSPUI = templist;
}

当选择gvSPU行时,它应该将所有记录存储到临时列表中并以gvFinalised显示。

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

protected void lbnAdd_Click(object sender, EventArgs e)
{
    List<ProductPacking> prodVariantDetail = new List<ProductPacking>();

    int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

    // get the last product variant IDs from ViewState
    prodVariantIDList = this.SelectedVariantDetailIDs;

    foreach (RepeaterItem ri in Repeater1.Items)
    {
        GridView gvProduct = (GridView)ri.FindControl("gvProduct");
        foreach (GridViewRow gr in gvProduct.Rows)
        {
            CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
            //Prevent gvFinalised to store duplicate products
            if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
            {
                // add the corresponding DataKey to idList
                prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
            }
        }
    }

    for (int i = 0; i < prodVariantIDList.Count; i++)
    {
        prodVariantDetail.Add(prodPackBLL.getProdVariantDetailByID(prodVariantIDList[i]));
    }

    gvFinalised.DataSource = prodVariantDetail;
    gvFinalised.DataBind();

    // save prodVariantIDList to ViewState
    this.SelectedVariantDetailIDs = prodVariantIDList;
}

当点击添加按钮时,它应该与gridview中的记录一起附加,而不是擦掉它们并插入选中的记录。

我使用viewState在回发之间存储记录。但是,当我尝试将新项目添加到gvFinalised时,临时列表中的前一条记录将消失,gvFinalised将填充prodVariantDetail列表中的结果。

任何指南?提前谢谢。

修改

List<DistributionStandardPackingUnitItems> itemList = new List<DistributionStandardPackingUnitItems>();
protected void gvSPU_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

        int rowNo = int.Parse(e.CommandArgument.ToString());
        SPUname = this.gvSPU.DataKeys[rowNo].Value.ToString();
        lblSPUname.Text = SPUname;

        itemList = tempDistSPUI;

        itemList = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);
        gvFinalised.DataSource = itemList;
        gvFinalised.DataBind();

        this.tempDistSPUI = itemList;
    }

    protected void lbnAdd_Click(object sender, EventArgs e)
    {
        List<DistributionStandardPackingUnitItems> prodVariantDetail = new List<DistributionStandardPackingUnitItems>();

        int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

        // get the last product variant IDs from ViewState
        prodVariantIDList = this.SelectedVariantDetailIDs;

        foreach (RepeaterItem ri in Repeater1.Items)
        {
            GridView gvProduct = (GridView)ri.FindControl("gvProduct");
            foreach (GridViewRow gr in gvProduct.Rows)
            {
                CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
                //Prevent gvFinalised to store duplicate products
                if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
                {
                    // add the corresponding DataKey to idList
                    prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
                }
            }
        }

        for (int i = 0; i < prodVariantIDList.Count; i++)
        {
            prodVariantDetail.Add(packBLL.getProdVariantDetailByID(prodVariantIDList[i]));
        }

        for (int j = 0; j < itemList.Count; j++)
        {
            prodVariantDetail.Add(itemList[j]);
        }

        gvFinalised.DataSource = prodVariantDetail;
        gvFinalised.DataBind();

        }

        // save prodVariantIDList to ViewState
        this.SelectedVariantDetailIDs = prodVariantIDList;
    }

1 个答案:

答案 0 :(得分:1)

在将tempDistSPUI绑定到prodVariantDetail之前,您必须将prodVariantDetail的元素添加到gvFinalised

以下是我使用List.AddRange Method做的事情:

protected void lbnAdd_Click(object sender, EventArgs e)
{
    List<DistributionStandardPackingUnitItems> prodVariantDetail = new List<DistributionStandardPackingUnitItems>();

    int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

    // get the last product variant IDs from ViewState
    prodVariantIDList = this.SelectedVariantDetailIDs;

    foreach (RepeaterItem ri in Repeater1.Items)
    {
        GridView gvProduct = (GridView)ri.FindControl("gvProduct");
        foreach (GridViewRow gr in gvProduct.Rows)
        {
            CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
            //Prevent gvFinalised to store duplicate products
            if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
            {
                // add the corresponding DataKey to idList
                prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
            }
        }
    }

    for (int i = 0; i < prodVariantIDList.Count; i++)
    {
        prodVariantDetail.Add(packBLL.getProdVariantDetailByID(prodVariantIDList[i]));
    }

    // get the content of tempDistSPUI from ViewState
    itemList = this.tempDistSPUI;

    // add all elements of itemList to prodVariantDetail
    prodVariantDetail.AddRange(itemList);

    gvFinalised.DataSource = prodVariantDetail;
    gvFinalised.DataBind();

    // save prodVariantIDList to ViewState
    this.SelectedVariantDetailIDs = prodVariantIDList;
}