C# 加载、更新和显示通用列表类
目标记住,这个概念有点像购物车,所以当他们向列表(细节)添加项目时,它会将他们添加的项目保留在内存中。 当我第一次加载列表(网格)并添加更多行时,这种方法就起作用了。但是如果我设置第一行,设置项目和价格,然后决定再添加3行,那么 我添加的信息将被删除,而不是保留其值,只需将更多行加载到列表中即可重新填充gridview。 在过去,我对datatables做过这样的操作,但我希望能够从中移动并使用这个List类 我还将其设置为viewstate,以便在整个页面中使用它C# 加载、更新和显示通用列表类,c#,asp.net,list,generic-list,C#,Asp.net,List,Generic List,目标记住,这个概念有点像购物车,所以当他们向列表(细节)添加项目时,它会将他们添加的项目保留在内存中。 当我第一次加载列表(网格)并添加更多行时,这种方法就起作用了。但是如果我设置第一行,设置项目和价格,然后决定再添加3行,那么 我添加的信息将被删除,而不是保留其值,只需将更多行加载到列表中即可重新填充gridview。 在过去,我对datatables做过这样的操作,但我希望能够从中移动并使用这个List类 我还将其设置为viewstate,以便在整个页面中使用它 private ListAr
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));
}
私有列表文档详细信息
{
得到
{
ListArDocumentdetail _detail=新ListArDocumentdetail();
if(ViewState[“Detail”]!=null)
{
_详细信息=(ListArDocumentdetail)视图状态[“详细信息”];
}
返回详细信息;
}
设置
{
ViewState[“详细信息”]=值;
}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
//创建两行以开始
创建行(2);
}
public void CreateRows(int rowstoadd)
{
int newtotalrows=Detail.Count+rowstoadd-1;
for(int i=Detail.Count;i 0)
详细信息。插入(详细信息。计数,详细信息);
其他的
细节。添加(细节);
细节=细节;
}
gvInvoiceDetail.DataSource=详细信息;
gvInvoiceDetail.DataBind();
GridViewRow row=gvInvoiceDetail.Rows[gvInvoiceDetail.Rows.Count-1];
ImageButton btnAdd=(ImageButton)行。FindControl(“btnAdd”);
btnAdd.Visible=true;
}
受保护的无效ibAdd_单击(对象发送者,ImageClickEventArgs e)
{
//用户可以输入要添加到当前行数上的行数
//因此,由于网格从2开始,并键入3,网格将刷新为5行。
CreateRows(Convert.ToInt32(txtRows.Text));
}
受保护的void UpdateRow(对象发送方,事件参数e)
{
ImageButton btnUpdate=发送方作为ImageButton;
GridViewRow行=btnUpdate.NamingContainer作为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);
详细信息[index].Itemid=Convert.ToInt32(ddlDescription.SelectedValue);
详细信息[索引].Price=Convert.ToDecimal(txtPrice.Text);
详细信息[索引]。小计=Convert.ToDecimal(详细信息[索引]。价格*Convert.ToInt32(txtQuantity.Text));
}
我可以向您推荐以下逻辑:
我可以向你提出这样的逻辑:
漂亮男人谢谢!另一个注意事项是,在我的gridview rowdatabound中,我没有在viewstate中设置列表中的值……这是我的错……对不起,我没有理解您的意思。你能详细说明一下你的情况吗。你的逻辑帮助我正确添加/更新,但我做错的是,当数据绑定我的网格时,我正确设置了控件的值。就像我的项目下拉列表一样,我没有获取存储在列表中的值,所以它总是显示为空。谢谢!另一个注意事项是,在我的gridview rowdatabound中,我没有在viewstate中设置列表中的值……这是我的错……对不起,我没有理解您的意思。你能详细说明一下你的情况吗。你的逻辑帮助我正确添加/更新,但我做错的是,当数据绑定我的网格时,我正确设置了控件的值。就像我的项目下拉列表一样,我没有获取列表中存储的值,所以它总是显示为空。谢谢