加载,更新和显示通用列表类

时间:2011-12-07 17:42:32

标签: c# asp.net list generic-list

目标,这个概念有点像购物车,因此当他们将项目添加到列表(详细信息)时,它会将他们添加的项目保留在内存中。 这在我第一次加载列表(网格)并添加更多行时起作用。但是,如果我设置第一行并设置项目和价格,然后决定再添加3行 我添加的信息被删除而不是保留其值,只是将更多行加载到列表中,这将重新填充gridview。 在过去,我已经使用数据表完成了这项工作,但我希望能够从中移动并使用此List Class 我也把它设置为viewstate所以我可以在我的页面中使用它。

private ListArDocumentdetail Detail
{
    get
    {
        ListArDocumentdetail _detail = new ListArDocumentdetail();
        if (ViewState["Detail"] != null)
        {
            _detail = (ListArDocumentdetail)ViewState["Detail"];    
        }
        return _detail;
     }
     set
     {
        ViewState["Detail"] = value;
     }
}
protected void Page_Load(object sender, EventArgs e)
{
    //creates 2 rows to start off
    CreateRows(2);
}
public void CreateRows(int rowstoadd)
{
    int newtotalrows = Detail.Count + rowstoadd - 1;
    for (int i = Detail.Count; i <= newtotalrows; i++)
    {
        ArDocumentdetail detail = new ArDocumentdetail();
        detail.Lineid = i;
        detail.Itemid = 0;
        detail.Quantity = 1;
        if (Detail.Count > 0)
            Detail.Insert(Detail.Count, detail);
        else
            Detail.Add(detail);

        Detail = Detail;
    }
    gvInvoiceDetail.DataSource = Detail;
    gvInvoiceDetail.DataBind();

    GridViewRow row = gvInvoiceDetail.Rows[gvInvoiceDetail.Rows.Count - 1];
    ImageButton btnAdd = (ImageButton)row.FindControl("btnAdd");
    btnAdd.Visible = true;
}
protected void ibAdd_Click(object sender, ImageClickEventArgs e)
{
    //user can type in how many rows they want to add on to current amount of rows
    //so since grid starts off at 2 and they type 3 the grid refreshes with 5 rows.
    CreateRows(Convert.ToInt32(txtRows.Text));
}

protected void UpdateRow(object sender, EventArgs e)
{
    ImageButton btnUpdate = sender as ImageButton;
    GridViewRow row = btnUpdate.NamingContainer as GridViewRow;

    TextBox txtPrice = (TextBox)row.FindControl("txtPrice");
    TextBox txtQuantity = (TextBox)row.FindControl("txtQuantity");
    DropDownList ddlDescription = (DropDownList)row.FindControl("ddlDescription");

    int index = Detail.FindIndex(f => f.Lineid == row.RowIndex);
    Detail[index].Itemid = Convert.ToInt32(ddlDescription.SelectedValue);
    Detail[index].Price = Convert.ToDecimal(txtPrice.Text);
    Detail[index].Subtotal = Convert.ToDecimal(Detail[index].Price * Convert.ToInt32(txtQuantity.Text));

}

1 个答案:

答案 0 :(得分:1)

我可以建议你逻辑:

  1. 将列表推入视图状态,例如Viewstate [“List”],
  2. 让用户选择一个项目。然后列表列表=(列表)Viewstate [“List”];
  3. 将所选项目添加到“列表”列表中。即list.Add(item);
  4. 现在将项目推回到viewstate。 Viewstate [“list”] = list;
  5. 将其绑定到网格或在页面上显示。无论你想要什么。