积极答复者
ListView分页查询问题

问题
-
大家好:
有个ListView分页查询的问题想请教一下。谢谢。
------------------------简介--------------------------------------
数据绑定,查询的实现如下:
ListView默认绑定到一个ObjectDataSource上(进入查询页面时显示的就是这个objectdatasource中数据)。
使用该ObjectDataSource实现了高效分页。
查询使用LINQ to Entity实现,然后把结果绑定当ListView.DataSource上。
-------------------------------问题描述-----------------------------------------------
进入“查询页面”后,在“所有人”文本框中输入“徐星”,点击“查询”按钮。ListView正确显示记录,共有1000多条数据,分为101页。此时,点击ListView中的页码(如页码5),ListView显示第五页的数据(这个没有问题)。在第五页时,再“RFID编号”文本框中输入“RFID00002”,点击“查询按钮”,ListView显示“没有记录”。
而事实上,数据库中有这样的数据(所有人为徐星,RFID编号包含RFID00002(RFID000021,RFID000022.。。。。。))10条。
但是得到查询结果时(文本框中输入:徐星),如果“不点击”页码,而直接在“默认的”页码上(第1页)中,输入RFID00002后,点击查询,ListView是能显示数据的(10条)。
我错在哪里??
--------------------------调试结果-----------------------------------------------------
在调试时,在“查询按钮”的方法中设置了断点,发现在第五页时输入的查询条件RFID00002被代码“成功”的获取了,使用LINQ to Entity与查询条件也得出了正确的结果:10条记录。
为什么结果没有绑定到ListView上??
同时,我发现在执行ListView1.DataBind()时,代码会首先跳到ListView的DataBound事件处理函数中(我没有在其中编写任何代码),然后在跳转回ListView1.DataBind()进行执行。 难道和这个有关系???疑惑啊!!
还有,当点击ListView的页码时,为什么代码会走到Page_Load(){}的
if (!IsPostBack){......}
代码块中,点击页码不是应该发生PostBack的吗??
-----------------------代码----------------------------------------------------
Session["LVQueryString"]的作用是:在查询完成前,保存查询条件。当页面跳转时,就可以使ListView显示查询结果集
而不是原始的未查询的记录。比如:首先在“查询页面”根据某个条件进行查询后,点击“编辑按钮”页面跳转到了“编辑页面”,编辑完成后,“编辑页面”自动跳转回“查询页面”。此时,Session["LVQueryString"]就可以保证ListView显示的是原来的
查询结果集。
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { AssetsManageSysEntities db = new AssetsManageSysEntities(); //SystemDictionaryTableAdapter sdadapter = new SystemDictionaryTableAdapter(); #region 在ProductSearch.aspx保留查询记录 if (Session["LVQueryString"] != null) { Expression<Func<AssetsMain, bool>> queryString = (Expression<Func<AssetsMain, bool>>)Session["LVQueryString"]; var queryresult = db.AssetsMain.Where(queryString.Compile()).Select(a => a).ToList(); ListView1.DataSourceID = ""; ListView1.DataSource = queryresult; ListView1.DataBind(); } #endregion } else { //isPostBack } }
protected void lkbtSearch_Click(object sender, EventArgs e) { #region 使用LINQ查询 if (txtRFIDCode.Text != "") { ExpWhere = ExpWhere.And(p => p.RFID.Contains(txtRFIDCode.Text));} if (txtProdID.Text != "") {ExpWhere = ExpWhere.And(p => p.ProductID.Contains(txtProdID.Text));} if (txtProdName.Text != "") { ExpWhere = ExpWhere.And(p=>p.ProductName.Contains(txtProdName.Text)); } if (txtProdType.Text != "") { ExpWhere = ExpWhere.And(p => p.ProductVesion.Contains(txtProdType.Text)); } if (txtOwner.Text != "") {ExpWhere = ExpWhere.And(p=>p.EmployeeName.Contains(txtOwner.Text));} if (txtManufactory.Text != "") { ExpWhere = ExpWhere.And(p=>p.ManufactoryName.Contains(txtManufactory.Text)); } if (txtStockInTime.Text != "") { DateTime stockInTime = Convert.ToDateTime(txtStockInTime.Text); ExpWhere = ExpWhere.And(p => p.StockinTime >= stockInTime); } if (txtStockOutTime.Text != "") { DateTime stockOutTime = Convert.ToDateTime(txtStockOutTime.Text); ExpWhere = ExpWhere.And(p=>p.StockoutTime<= stockOutTime); } if (ddlProFirstCate.SelectedValue != "") {ExpWhere = ExpWhere.And(p => p.FirstCategory.Contains(ddlProFirstCate.SelectedValue));} if (ddlProSecondCate.SelectedValue != "") { ExpWhere = ExpWhere.And(p=>p.SecodeCategory.Contains(ddlProSecondCate.SelectedValue)); } if (ddlProThirdCate.SelectedValue != "") { ExpWhere = ExpWhere.And(p => p.ThirdCategory.Contains(ddlProThirdCate.SelectedValue)); } if(ddlLocation.SelectedValue!="") {ExpWhere = ExpWhere.And(p=>p.Location.Contains(ddlLocation.SelectedItem.ToString())); } #endregion var queryresult = db.AssetsMain.Where(ExpWhere.Compile()).Select(a=>a).ToList(); ListView1.DataSourceID = ""; ListView1.DataSource = queryresult; ListView1.DataBind(); Session["LVQueryString"] = ExpWhere; }
答案
-
这个问题跟你前面一个问题是一个性质的。
都是在页面重新翻页后 没有绑定到新的数据源。
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Mike FengModerator 2012年5月31日 11:28
全部回复
-
这个问题跟你前面一个问题是一个性质的。
都是在页面重新翻页后 没有绑定到新的数据源。
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Mike FengModerator 2012年5月31日 11:28