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

发布于 2024-12-20 12:52:56 字数 2379 浏览 4 评论 0原文

记住目标,这个概念有点像购物车,因此当他们将项目添加到列表(详细信息)时,它会将他们添加的项目保留在内存中。 当我第一次加载列表(网格)并添加更多行时,这会起作用。但是如果我设置第一行并设置商品和价格,然后决定再添加 3 行 我添加的信息被删除,而不是保留其值,而是将更多行加载到列表中,这将重新填充网格视图。 过去我曾使用数据表完成此操作,但我希望能够摆脱它并使用此列表类 我还将它设置为视图状态,这样我就可以在我的页面中使用它。

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));

}

Goal in mind, the concept is kind of like a shopping cart, so as they add items to list(Detail) it keeps the items they are adding in memory.
This works when ever I first load the list(grid) and add more rows. But if I set the first row and set the item and price and then decide to add 3 more rows then
the info I had added gets deleted instead of keeping its values and just load more lines to the list which would repopulate the gridview.
In the past I have done this with datatables but I want to be able to move from that and use this List Class
Also I have it set as viewstate so I can use it through out my page.

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));

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2024-12-27 12:52:56

我可以建议您的逻辑:

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

I can suggest you the logic:

  1. Push a list into viewstate say Viewstate["List"],
  2. Let a user chose an item. Then List list = (List)Viewstate["List"];
  3. Add the selected item to List list. i.e. list.Add(item);
  4. Now push the item back to viewstate. Viewstate["list"] = list;
  5. Bind it to grid or display it on page. Whatever you want.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文