加载、更新和显示泛型列表类
本文关键字:泛型 列表 显示 更新 加载 | 更新日期: 2023-09-27 18:17:00
目标,这个概念有点像购物车,所以当他们向list(Detail)添加物品时,它会将他们添加的物品保存在内存中。当我第一次加载列表(网格)并添加更多行时,这种方法就会起作用。但是如果我设置了第一行,设置了商品和价格,然后决定再添加3行我添加的信息被删除,而不是保持它的值,只是加载更多的行列表,这将重新填充gridview。在过去,我已经用数据表做到了这一点,但我希望能够从中移动并使用这个列表类我还把它设置为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));
}
我可以建议你的逻辑:
- Push list into viewstate说viewstate [" list "],
- 允许用户选择项目。然后List List = (List)Viewstate["List"];
- 将选中的项目添加到列表列表中。即list.Add(项);
- 现在将项目推回viewstate。Viewstate["list"] = list;
- 将其绑定到网格或显示在页面上。