向ViewState添加更多记录而不覆盖以前的记录 [英] Adding more records to ViewState without Overridding Previous records

查看:101
本文介绍了向ViewState添加更多记录而不覆盖以前的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在编译结束之前在视图状态中添加更多记录,直到DataBind()。代码工作正常,但是当我再次启动进程时,它会从视图状态中删除以前的记录,我希望合并视图状态而不是覆盖任何想法..





i want to add more records in a viewstate, after compiling to end till DataBind(). the code works fine, but when i again start the process, it remove the previous record from view state, i want view state to be merged not override any ideas..


CheckBox cBox2 = null;
            for (int i = 0; i < lvItemTone2.Items.Count(); i++)
            {
                cBox2 = (CheckBox)lvItemTone2.Items[i].FindControl("lvTone2chckSelect");
                if (cBox2.Checked)
                {
                    ItemCode = ((DropDownList)GridView1.FooterRow.FindControl("ddlItemCode")).SelectedItem.Text.ToString();
                    BatchNo = ((Label)lvItemTone2.Items[i].FindControl("lblBatchNo")).Text;
                    Quantity = ((Label)lvItemTone2.Items[i].FindControl("lblQty")).Text;
                    SelectedQuantity = ((Label)lvItemTone2.Items[i].FindControl("lblSelQty")).Text;

                    dr = dt.NewRow();
                    dr["ItemCode"] = ItemCode;
                    dr["BatchNo"] = BatchNo;
                    dr["Quantity"] = Quantity;
                    dr["SelectedQuantity"] = SelectedQuantity;
                    
                    if (dr != null)
                    {
                        dt.Rows.Add(dr);
                    }
                }
            }
            ViewState["BatchTable"] = dt;
            lvItemTone2.DataSource = dt;
            lvItemTone2.DataBind();

推荐答案

嘿那里,



你需要检查 ViewState 中是否存在 DataTable ,如果是,则使用那个而不是创建一个每次都有新的。在上面的某个地方创建 DataTable 对象循环之后使用这样的东西(你没有发布那个部分),那个将方式添加到ViewState中以前保存的DataTable:



Hey there,

You need check if the DataTable exists in the ViewState, if yes then use that one instead of creating a new one every time. Use something like this after creating the DataTable object somewhere above for loop (You didn't post that part), that way will adding Rows to previously saved DataTable in the ViewState:

if(ViewState["BatchTable"] != null)
{
    dt = (DataTable)ViewState["BatchTable"];
}





希望它有所帮助,如果不是您想要的,请告诉我。



Azee ......



Hope it helps, do let me know if it isn't what you were looking for.

Azee...


1。将viewstate以前的数据复制到名为(preDt)的数据表中。

2.将新数据表(newDt)与preDt合并。

3.现在将输出结果设置为view state。
1. copy the viewstate previous data into a datatable named(preDt).
2. merge the new datatable (newDt) with preDt.
3. now set the output result to view state.


在这样的加载时创建你的视图状态

create your viewstate at load time like this
protected void Page_Load(object sender, EventArgs e)
       {
if (!IsPostBack)
           {
datatatle dt=new DataTable();
dt.column.add("BatchNo");
dt.column.add("ItemCode");
dt.column.add("Quantity");
dt.column.add("SelectedQuantity");

ViewState["data"]=dt;
}
}

put your code like this

CheckBox cBox2 = null;
DataTable dt=ViewsState["data"] as DataTable;
            for (int i = 0; i < lvItemTone2.Items.Count(); i++)
            {
                cBox2 = (CheckBox)lvItemTone2.Items[i].FindControl("lvTone2chckSelect");
                if (cBox2.Checked)
                {
                    ItemCode = ((DropDownList)GridView1.FooterRow.FindControl("ddlItemCode")).SelectedItem.Text.ToString();
                    BatchNo = ((Label)lvItemTone2.Items[i].FindControl("lblBatchNo")).Text;
                    Quantity = ((Label)lvItemTone2.Items[i].FindControl("lblQty")).Text;
                    SelectedQuantity = ((Label)lvItemTone2.Items[i].FindControl("lblSelQty")).Text;
 
                    dr = dt.NewRow();
                    dr["ItemCode"] = ItemCode;
                    dr["BatchNo"] = BatchNo;
                    dr["Quantity"] = Quantity;
                    dr["SelectedQuantity"] = SelectedQuantity;
                    
                    if (dr != null)
                    {
                        dt.Rows.Add(dr);
                    }
                }
            }
            //ViewState["BatchTable"] = dt;
            lvItemTone2.DataSource = dt;
            lvItemTone2.DataBind();





此代码绝对适合你



this code definitely work for you


这篇关于向ViewState添加更多记录而不覆盖以前的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆