一个全手工绑定的异步回调公共搜索窗口
由于系统中的公共搜索窗口太慢,就重写了公共搜索窗口。采用了异步回调和Session缓存数据,并全手工绑定。因为要兼容原系统中的原来的一大堆JS调用和存储过程(几百个)。所以这个搜索窗口并不是最优的,例如无法采用存储过程分页等更优的方法。但一定程度上缓解了系统的搜索窗口反应慢问题......
由于系统中的公共搜索窗口太慢,就重写了公共搜索窗口。采用了异步回调和Session缓存数据,并全手工绑定。因为要兼容原系统中的原来的一大堆JS调用和存储过程(几百个)。所以这个搜索窗口并不是最优的,例如无法采用存储过程分页等更优的方法。但一定程度上缓解了系统的搜索窗口反应慢问题。在这里记录一下:
主要绑定函数:
主要分页函数:
更详细请见完整代码:
完整代码
主要绑定函数:
绑定代码
/**//// <summary>
/// Bound Data
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
private string DataBound(DataTable dt)
{
try
{
string strReturn = string.Empty;
//checkbox or radiobox
bool isCheckBox = true;
if (Request["SelectType"] == null || Request["SelectType"] == "0")
{
isCheckBox = false;
}
int PageAmount = Convert.ToInt32(Request["PageAmount"]);
if (PageAmount == 0)
{
PageAmount = 15;
}
int PageIndex = Convert.ToInt32(Request["PageIndex"]);
int MaxPageIndex = GetMaxPageIndex(PageAmount, dt.Rows.Count);
if (PageIndex > MaxPageIndex)
{
PageIndex = MaxPageIndex;
}
if (dt != null && dt.Columns.Count > 0 && dt.Rows.Count > 0)
{
int ColumnsCount = dt.Columns.Count;
int RowsCount = dt.Rows.Count;
//header
StringBuilder strbContent = new StringBuilder();
strbContent.Append("<table id=\"tableContent\" style=\"width:100%;background-color:#FFFFFF;\" bordercolordark=\"#FFFFFF\" border=\"1\" bordercolorlight=\"#CCCCCC\"><tr id=\"trHeader\" class=\"cssDataGrid_Header\" style=\"line-height:20px;\">");
if (isCheckBox)
{
strbContent.Append("<td><input id=\"CheckAll\" onclick=\"ChkAll()\" name=\"CheckAll\" type=\"checkbox\" /></td>");
}
else
{
strbContent.Append("<td><span style=\"display:none;\"> </span></td>");
}
//the first columns is return value,need hide.
for (int i = 1; i < ColumnsCount; i++)
{
strbContent.Append(string.Format("<td style=\"white-space:nowrap;\">{0}</td>", dt.Columns[i].ColumnName));
}
strbContent.Append("</tr>");
strReturn += strbContent.ToString();
//content
strbContent = new StringBuilder();
for (int i = PageIndex * PageAmount; i < (PageIndex * PageAmount + PageAmount); i++)
{
if (i == dt.Rows.Count)
{
break;
}
if (i % 2 == 0)
{
strbContent.Append(string.Format("<tr id=\"tr{0}\" class=\"cssDataGrid_Item\" style=\"line-height:20px;\">", i.ToString()));
}
else
{
strbContent.Append(string.Format("<tr id=\"tr{0}\" class=\"cssDataGrid_AlternatingItem\" style=\"line-height:20px;\">", i.ToString()));
}
if (isCheckBox)
{
strbContent.Append(string.Format("<td><input id=\"chk{0}\" name=\"chk\" onclick=\"setReturnValue(this)\" type=\"checkbox\" /></td>", i.ToString()));
}
else
{
strbContent.Append(string.Format("<td><input id=\"rdo{0}\" name=\"rdo\" onclick=\"setReturnValue(this)\" type=\"radio\" /></td>", i.ToString()));
}
for (int j = 0; j < ColumnsCount; j++)
{
//the first columns is return value,need hide.
if (j == 0)
{
strbContent.Append(string.Format("<td id=\"td{0}\" style=\"display:none;\">{1}</td>", i.ToString() + j.ToString(), dt.Rows[i][j].ToString().Replace("\0", "")));
}
else
{
strbContent.Append(string.Format("<td id=\"td{0}\" style=\"white-space:nowrap;\">{1}<span style=\"display:none;\"> </span></td>", i.ToString() + j.ToString(), dt.Rows[i][j].ToString().Replace("\0", "")));
}
}
strbContent.Append("</tr>");
}
strbContent.Append("</table>");
strReturn += strbContent.ToString();
//footer
strReturn += string.Format("<table id=\"tablePages\" class=\"cssDataGrid_AlternatingItem\" style=\"width:100%;\">{0}</table>", GetPagination(PageIndex, PageAmount, dt.Rows.Count));
}
else
{
strReturn = "<table id=\"tableContent\" style=\"width:100%;font-weight:bold;color:Red;\"><tr><td>No Data!</td></tr></table>";
}
return strReturn;
}
catch (Exception exp)
{
throw exp;
}
}
/**//// <summary>
/// Bound Data
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
private string DataBound(DataTable dt)
{
try
{
string strReturn = string.Empty;
//checkbox or radiobox
bool isCheckBox = true;
if (Request["SelectType"] == null || Request["SelectType"] == "0")
{
isCheckBox = false;
}
int PageAmount = Convert.ToInt32(Request["PageAmount"]);
if (PageAmount == 0)
{
PageAmount = 15;
}
int PageIndex = Convert.ToInt32(Request["PageIndex"]);
int MaxPageIndex = GetMaxPageIndex(PageAmount, dt.Rows.Count);
if (PageIndex > MaxPageIndex)
{
PageIndex = MaxPageIndex;
}
if (dt != null && dt.Columns.Count > 0 && dt.Rows.Count > 0)
{
int ColumnsCount = dt.Columns.Count;
int RowsCount = dt.Rows.Count;
//header
StringBuilder strbContent = new StringBuilder();
strbContent.Append("<table id=\"tableContent\" style=\"width:100%;background-color:#FFFFFF;\" bordercolordark=\"#FFFFFF\" border=\"1\" bordercolorlight=\"#CCCCCC\"><tr id=\"trHeader\" class=\"cssDataGrid_Header\" style=\"line-height:20px;\">");
if (isCheckBox)
{
strbContent.Append("<td><input id=\"CheckAll\" onclick=\"ChkAll()\" name=\"CheckAll\" type=\"checkbox\" /></td>");
}
else
{
strbContent.Append("<td><span style=\"display:none;\"> </span></td>");
}
//the first columns is return value,need hide.
for (int i = 1; i < ColumnsCount; i++)
{
strbContent.Append(string.Format("<td style=\"white-space:nowrap;\">{0}</td>", dt.Columns[i].ColumnName));
}
strbContent.Append("</tr>");
strReturn += strbContent.ToString();
//content
strbContent = new StringBuilder();
for (int i = PageIndex * PageAmount; i < (PageIndex * PageAmount + PageAmount); i++)
{
if (i == dt.Rows.Count)
{
break;
}
if (i % 2 == 0)
{
strbContent.Append(string.Format("<tr id=\"tr{0}\" class=\"cssDataGrid_Item\" style=\"line-height:20px;\">", i.ToString()));
}
else
{
strbContent.Append(string.Format("<tr id=\"tr{0}\" class=\"cssDataGrid_AlternatingItem\" style=\"line-height:20px;\">", i.ToString()));
}
if (isCheckBox)
{
strbContent.Append(string.Format("<td><input id=\"chk{0}\" name=\"chk\" onclick=\"setReturnValue(this)\" type=\"checkbox\" /></td>", i.ToString()));
}
else
{
strbContent.Append(string.Format("<td><input id=\"rdo{0}\" name=\"rdo\" onclick=\"setReturnValue(this)\" type=\"radio\" /></td>", i.ToString()));
}
for (int j = 0; j < ColumnsCount; j++)
{
//the first columns is return value,need hide.
if (j == 0)
{
strbContent.Append(string.Format("<td id=\"td{0}\" style=\"display:none;\">{1}</td>", i.ToString() + j.ToString(), dt.Rows[i][j].ToString().Replace("\0", "")));
}
else
{
strbContent.Append(string.Format("<td id=\"td{0}\" style=\"white-space:nowrap;\">{1}<span style=\"display:none;\"> </span></td>", i.ToString() + j.ToString(), dt.Rows[i][j].ToString().Replace("\0", "")));
}
}
strbContent.Append("</tr>");
}
strbContent.Append("</table>");
strReturn += strbContent.ToString();
//footer
strReturn += string.Format("<table id=\"tablePages\" class=\"cssDataGrid_AlternatingItem\" style=\"width:100%;\">{0}</table>", GetPagination(PageIndex, PageAmount, dt.Rows.Count));
}
else
{
strReturn = "<table id=\"tableContent\" style=\"width:100%;font-weight:bold;color:Red;\"><tr><td>No Data!</td></tr></table>";
}
return strReturn;
}
catch (Exception exp)
{
throw exp;
}
}
主要分页函数:
分页代码
/**//// <summary>
/// Here get the pagination string
/// </summary>
/// <param name="PageIndex"></param>
/// <param name="PageAmount"></param>
/// <param name="RecordCount"></param>
/// <returns></returns>
private string GetPagination(int PageIndex, int PageAmount, int RecordCount)
{
try
{
string PageName = "Search.aspx";
string PageParameterName = "PageIndex";
string Footer = string.Empty;
string FirstPage = "0";
string PrevPage = GetPrevPage(PageIndex);
string NextPage = GetNextPage(PageIndex, PageAmount, RecordCount);
string LastPage = GetMaxPageIndex(PageAmount, RecordCount).ToString();
string CurrentInfo = GetCurrentInfo(PageIndex, PageAmount, RecordCount);
int CurrentPageIndex = PageIndex + 1;
Footer += "<tr style=\"line-height:20px;\"><td align=\"left\">";
Footer += string.Format("<a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({0})\">First</a> <a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({1})\">Prev</a> ", FirstPage, PrevPage);
Footer += string.Format("<a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({0})\">Next</a> <a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({1})\">Last</a> ", NextPage, LastPage);
Footer += string.Format("<input type=\"text\" class=\"cssinputbox\" size=\"2\" maxlength=\"4\" id=\"txtPagination\" value=\"{0}\" ", CurrentPageIndex.ToString());
Footer += "onkeyup=\"value=value.replace(/[^\\d]/g,'') \" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\" /> ";
Footer += "<input type=\"button\" class=\"cssButton\" id=\"btnPagination\" size=\"2\" value=\"Go\" onclick=\"btnPagination_OnClick()\" /> ";
Footer += string.Format("{0}</td></tr>", CurrentInfo);
return Footer;
}
catch (Exception exp)
{
throw exp;
}
}
/**//// <summary>
/// prev page
/// </summary>
/// <param name="PageIndex"></param>
/// <returns></returns>
private string GetPrevPage(int PageIndex)
{
try
{
PageIndex = PageIndex - 1;
if (PageIndex < 0)
{
PageIndex = 0;
}
return PageIndex.ToString();
}
catch (Exception exp)
{
throw exp;
}
}
/**//// <summary>
/// next page
/// </summary>
/// <param name="PageIndex"></param>
/// <param name="PageAmount"></param>
/// <param name="RecordCount"></param>
/// <returns></returns>
private string GetNextPage(int PageIndex, int PageAmount, int RecordCount)
{
try
{
PageIndex = PageIndex + 1;
int MaxPageIndex = GetMaxPageIndex(PageAmount, RecordCount);
if (PageIndex > MaxPageIndex)
{
PageIndex = MaxPageIndex;
}
return PageIndex.ToString();
}
catch (Exception exp)
{
throw exp;
}
}
/**//// <summary>
/// current page info
/// </summary>
/// <param name="PageIndex"></param>
/// <param name="PageAmount"></param>
/// <param name="RecordCount"></param>
/// <returns></returns>
private string GetCurrentInfo(int PageIndex, int PageAmount, int RecordCount)
{
try
{
int MaxPageIndex = GetMaxPageIndex(PageAmount, RecordCount);
if (PageIndex > MaxPageIndex)
{
PageIndex = MaxPageIndex;
}
int FirstResult = PageAmount * PageIndex;
int LastResult = PageAmount * (PageIndex + 1);
if (LastResult > RecordCount)
{
LastResult = RecordCount;
}
return string.Format("Results {0} - {1} of {2} ", FirstResult.ToString(), LastResult.ToString(), RecordCount.ToString());
}
catch (Exception exp)
{
throw exp;
}
}
/**//// <summary>
/// get max page index
/// </summary>
/// <param name="PageAmount"></param>
/// <param name="RecordCount"></param>
/// <returns></returns>
private int GetMaxPageIndex(int PageAmount, int RecordCount)
{
try
{
float NearMaxPageIndex = (float)RecordCount / (float)PageAmount - 1;
return (int)Math.Ceiling(NearMaxPageIndex);
}
catch (Exception exp)
{
throw exp;
}
}
/**//// <summary>
/// Here get the pagination string
/// </summary>
/// <param name="PageIndex"></param>
/// <param name="PageAmount"></param>
/// <param name="RecordCount"></param>
/// <returns></returns>
private string GetPagination(int PageIndex, int PageAmount, int RecordCount)
{
try
{
string PageName = "Search.aspx";
string PageParameterName = "PageIndex";
string Footer = string.Empty;
string FirstPage = "0";
string PrevPage = GetPrevPage(PageIndex);
string NextPage = GetNextPage(PageIndex, PageAmount, RecordCount);
string LastPage = GetMaxPageIndex(PageAmount, RecordCount).ToString();
string CurrentInfo = GetCurrentInfo(PageIndex, PageAmount, RecordCount);
int CurrentPageIndex = PageIndex + 1;
Footer += "<tr style=\"line-height:20px;\"><td align=\"left\">";
Footer += string.Format("<a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({0})\">First</a> <a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({1})\">Prev</a> ", FirstPage, PrevPage);
Footer += string.Format("<a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({0})\">Next</a> <a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({1})\">Last</a> ", NextPage, LastPage);
Footer += string.Format("<input type=\"text\" class=\"cssinputbox\" size=\"2\" maxlength=\"4\" id=\"txtPagination\" value=\"{0}\" ", CurrentPageIndex.ToString());
Footer += "onkeyup=\"value=value.replace(/[^\\d]/g,'') \" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\" /> ";
Footer += "<input type=\"button\" class=\"cssButton\" id=\"btnPagination\" size=\"2\" value=\"Go\" onclick=\"btnPagination_OnClick()\" /> ";
Footer += string.Format("{0}</td></tr>", CurrentInfo);
return Footer;
}
catch (Exception exp)
{
throw exp;
}
}
/**//// <summary>
/// prev page
/// </summary>
/// <param name="PageIndex"></param>
/// <returns></returns>
private string GetPrevPage(int PageIndex)
{
try
{
PageIndex = PageIndex - 1;
if (PageIndex < 0)
{
PageIndex = 0;
}
return PageIndex.ToString();
}
catch (Exception exp)
{
throw exp;
}
}
/**//// <summary>
/// next page
/// </summary>
/// <param name="PageIndex"></param>
/// <param name="PageAmount"></param>
/// <param name="RecordCount"></param>
/// <returns></returns>
private string GetNextPage(int PageIndex, int PageAmount, int RecordCount)
{
try
{
PageIndex = PageIndex + 1;
int MaxPageIndex = GetMaxPageIndex(PageAmount, RecordCount);
if (PageIndex > MaxPageIndex)
{
PageIndex = MaxPageIndex;
}
return PageIndex.ToString();
}
catch (Exception exp)
{
throw exp;
}
}
/**//// <summary>
/// current page info
/// </summary>
/// <param name="PageIndex"></param>
/// <param name="PageAmount"></param>
/// <param name="RecordCount"></param>
/// <returns></returns>
private string GetCurrentInfo(int PageIndex, int PageAmount, int RecordCount)
{
try
{
int MaxPageIndex = GetMaxPageIndex(PageAmount, RecordCount);
if (PageIndex > MaxPageIndex)
{
PageIndex = MaxPageIndex;
}
int FirstResult = PageAmount * PageIndex;
int LastResult = PageAmount * (PageIndex + 1);
if (LastResult > RecordCount)
{
LastResult = RecordCount;
}
return string.Format("Results {0} - {1} of {2} ", FirstResult.ToString(), LastResult.ToString(), RecordCount.ToString());
}
catch (Exception exp)
{
throw exp;
}
}
/**//// <summary>
/// get max page index
/// </summary>
/// <param name="PageAmount"></param>
/// <param name="RecordCount"></param>
/// <returns></returns>
private int GetMaxPageIndex(int PageAmount, int RecordCount)
{
try
{
float NearMaxPageIndex = (float)RecordCount / (float)PageAmount - 1;
return (int)Math.Ceiling(NearMaxPageIndex);
}
catch (Exception exp)
{
throw exp;
}
}
更详细请见完整代码:
完整代码