一个全手工绑定的异步回调公共搜索窗口

本文介绍了一个改进的搜索窗口实现方案,通过异步回调和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;\">&nbsp;</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;\">&nbsp;</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>&nbsp;&nbsp;<a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({1})\">Prev</a>&nbsp;&nbsp;", FirstPage, PrevPage);
                Footer 
+= string.Format("<a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({0})\">Next</a>&nbsp;&nbsp;<a href=\"#\" style=\"font-weight:bold;\" onclick=\"lnkPagination_OnClick({1})\">Last</a>&nbsp;&nbsp;", 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,''))\" />&nbsp;&nbsp;";
                Footer 
+= "<input type=\"button\" class=\"cssButton\" id=\"btnPagination\" size=\"2\" value=\"Go\" onclick=\"btnPagination_OnClick()\" />&nbsp;&nbsp;";
                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;
            }

        }

更详细请见完整代码:
完整代码

转载于:https://www.cnblogs.com/KenBlove/archive/2008/07/09/1238648.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值