ListView 效率和显示序号的解决方案
做项目中,需要用ListView,以前用ASP开发,数据量大速度很慢。用asp.net,速度很快,近5万条记录2秒钟就显示完。经过今天的摸索,也解决了增加序号列的问题。我看网上很多文章都没有很深入的讲解。如果有朋友遇到类似的问题,联系交流。代码简单列一下,应该都能看懂,不明白再发帖。
public class DoyenItemTemplate:ITemplate
{
private string strPriConn, strPriListCode;
private string strListID;
private Boolean blnDisplayIndex;
int nIndex;
private string[] arrFields;
private int nFields;
public DoyenItemTemplate(string strConn, string strListCode)
{
strPriConn = strConn;
strPriListCode = strListCode;
blnDisplayIndex = false;
strListID = "";
CreateListInfo(strConn, strListCode);
}
private void CreateListInfo(string strConn, string strListCode)
{
string strSql;
string strFields = "";
SqlDataComm de = new SqlDataComm();
de.ConnStr = strPriConn;
de.Open();
// 获取列表ID
strSql = "SELECT Spl_ListID,Spl_IsDisplayIndex FROM S_PlatformList WHERE (Spl_Code = '" + strPriListCode + "')";
SqlDataReader dr = de.GetDataReader(strSql);
while (dr.Read())
{
strListID = dr["Spl_ListID"].ToString();
blnDisplayIndex = Convert.ToBoolean(dr["Spl_IsDisplayIndex"]);
}
dr.Close();
strSql = "SELECT Spl_FiledID, Spl_FieldName, Spl_DisplayName FROM S_PlatformListFields WHERE (Spl_IsDisplay=1) AND (Spl_ListID = '" + strListID + "') ORDER BY Spl_FieldIndex ";
dr = de.GetDataReader(strSql);
while (dr.Read())
{
nFields += 1;
if (strFields != "")
{
strFields += "$";
}
strFields += dr["Spl_FieldName"].ToString();
}
arrFields = strFields.Split('$');
dr.Close();
de.Close();
}
public void InstantiateIn(System.Web.UI.Control container)
{
PlaceHolder ph = new PlaceHolder();
TableRow row = new TableRow();
// 根据配置,生成列表模板
if (blnDisplayIndex)
{
row.Cells.Add(new TableCell() { ID = "DoyenIndex" });
}
for (int i = 0; i < nFields; i++)
{
row.Cells.Add(new TableCell() { ID = arrFields[i] });
}
ph.Controls.Add(row);
ph.DataBinding += new EventHandler(DoyenList_DataBinding);
container.Controls.Add(ph);
}
private void DoyenList_DataBinding(object sender, EventArgs e)
{
PlaceHolder ph = (PlaceHolder)sender;
IDataItemContainer ri = (IDataItemContainer)ph.NamingContainer;
object itemValue = null;
if (blnDisplayIndex)
{
//itemValue = DataBinder.Eval(ri.DataItem, nIndex.ToString());
((TableCell)ph.FindControl("DoyenIndex")).Text = nIndex.ToString();
nIndex += 1;
}
for (int i = 0; i < nFields; i++)
{
itemValue = DataBinder.Eval(ri.DataItem, arrFields[i]);
((TableCell)ph.FindControl(arrFields[i])).Text = itemValue.ToString();
}
}