asp.Netソース保存用プログラムページング

17120 ワード

ソース:
 
  
namespace Alex
{
public class PageTools
{
///
///
///

private string tableName;
public string TableName
{
get { return tableName; }
set { tableName = value; }
}
///
///
///

private string returnColumns;
public string ReturnColumns
{
get { return returnColumns; }
set { returnColumns = value; }
}
///
///
///

private string where;
public string Where
{
get { return where; }
set { where = value; }
}
///
///
///

private string orderColumnName;
public string OrderColumnName
{
get { return orderColumnName; }
set { orderColumnName = value; }
}
///
/// ,true
///

private Boolean orderDesc;
public Boolean OrderDesc
{
get { return orderDesc; }
set { orderDesc = value; }
}
///
///
///

private string keyColumnName;
public string KeyColumnName
{
get { return keyColumnName; }
set { keyColumnName = value; }
}
///
///
///

private Int32 pageSize;
public Int32 PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
///
///
///

private Int32 pageIndex;
public Int32 PageIndex
{
get { return pageIndex; }
set { pageIndex = value; }
}
///
/// SQL
///

private SqlParameter[] whereParams;
public SqlParameter[] WhereParams
{
get { return whereParams; }
set { whereParams = value; }
}
///
///
///
///
/// ,true
///
///
///
/// SQL
public PageTools(string tableName, string returnColumns, string where, string orderColumnName, Boolean orderDesc,
string keyColumnName, Int32 pageSize, Int32 pageIndex, params SqlParameter[] whereParams)
{
this.TableName = tableName;
this.ReturnColumns = returnColumns;
this.Where = where;
this.OrderColumnName = orderColumnName;
this.OrderDesc = orderDesc;
this.KeyColumnName = keyColumnName;
this.PageSize = pageSize;
this.PageIndex = pageIndex;
this.WhereParams = WhereParams;
}
public PageTools() { }
public DataTable RetrievePagerData()
{
if (pageIndex == 1)
{
return RetrieveTopData();
}
else
{
string sql = string.Format("select top {0} {1} from {2} where {3} and {6} not in ( select top {7} {6} from {2} where {3} order by {4} {5} ) order by {4} {5}", pageSize, returnColumns, tableName, where, orderColumnName, orderDesc == true ? "desc" : string.Empty, keyColumnName, pageSize * (pageIndex - 1));
return DbHelperSQL.Query(sql, CheckNull(whereParams)).Tables[0];
}
}
///
///
///

///
///
///
///
public Int32 RetrieveDataCount()
{
string sql = string.Format("select count(*) from {0} where {1}" , tableName, where);
Int32 result = (Int32)DbHelperSQL.GetSingle(sql, CheckNull(whereParams));
return result;
}
///
///
///

///
///
///
///
///
///
///
///
public DataTable RetrieveTopData()
{
string sql = string.Format("select top {0} {1} from {2} where {3} order by {4} {5}", pageSize, returnColumns, tableName, where, orderColumnName, orderDesc == true ? "desc" : string.Empty);
return DbHelperSQL.Query(sql, whereParams).Tables[0];
}
///
///
///

///
///
private SqlParameter[] CheckNull(SqlParameter[] paras)
{
if (paras == null)
{
return null;
}
List list = new List();
foreach (SqlParameter para in paras)
{
if (para != null) list.Add(para);
}
return list.ToArray();
}
}
}

ページバックグラウンド:
 
  
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
recordCount = page.RetrieveDataCount();
totalPages = (int)(Math.Ceiling((double)recordCount / (double)page.PageSize));
if (!IsPostBack)
{
BindData();
}
}
Alex.PageTools page = new Alex.PageTools("Test", "*", "1=1", "USER_Account", false, "USER_Account", 10, 1, null);
int recordCount;
public int totalPages;

private void BindData()
{
int pageIndex = SmartGridView1.PageIndex;
if(totalPages>0)
{
if(pageIndex>totalPages-1)
{
pageIndex=totalPages-1;
}
}
else
{
pageIndex=0;
}
SmartGridView1.PageIndex=pageIndex;
page.PageIndex=pageIndex+1;
SmartGridView1.DataSource = page.RetrievePagerData();
SmartGridView1.DataBind();
if (this.SmartGridView1.PageIndex == 0)
{
this.btnFirst.Enabled = false;
this.btnPrev.Enabled = false;
if (totalPages == 1)
{
this.btnLast.Enabled = false;
this.btnNext.Enabled = false;
}
}
else if (this.SmartGridView1.PageIndex == totalPages - 1)
{
this.btnLast.Enabled = false;
this.btnNext.Enabled = false;
}
this.lblpagesum.Text = totalPages.ToString();
this.lblpage.Text = (pageIndex + 1).ToString();
this.lblrowscount.Text = recordCount.ToString();
}
public void NavigateToPage(object sender, CommandEventArgs e)
{
btnFirst.Enabled = true;
btnPrev.Enabled = true;
btnNext.Enabled = true;
btnLast.Enabled = true;
string pageinfo = e.CommandArgument.ToString();
switch (pageinfo)
{
case "Prev":
if (this.SmartGridView1.PageIndex > 0)
{
this.SmartGridView1.PageIndex -= 1;
}
break;
case "Next":
if (this.SmartGridView1.PageIndex < (totalPages - 1))
{
this.SmartGridView1.PageIndex += 1;
}
break;
case "First":
this.SmartGridView1.PageIndex = 0;
break;
case "Last":
this.SmartGridView1.PageIndex = totalPages - 1;
break;
}
if (this.SmartGridView1.PageIndex == 0)
{
btnFirst.Enabled = false;
btnPrev.Enabled = false;
if (totalPages == 1)
{
btnLast.Enabled = false;
btnNext.Enabled = false;
}
}
else if (this.SmartGridView1.PageIndex == totalPages - 1)
{
btnLast.Enabled = false;
btnNext.Enabled = false;
}
BindData();
}
protected void gobtnClick(object sender, EventArgs e)
{
int goPageIndex = Convert.ToInt32(this.goCount.Text.Trim());
if (goPageIndex > 0)
{
if (goPageIndex >= totalPages)
{
this.SmartGridView1.PageIndex = totalPages - 1;
}
else
{
this.SmartGridView1.PageIndex = goPageIndex - 1;
}
}
else if(goPageIndex <=0)
{
this.SmartGridView1.PageIndex = 0;
}
if (this.SmartGridView1.PageIndex == 0)
{
btnFirst.Enabled = false;
btnPrev.Enabled = false;
btnLast.Enabled = true;
btnNext.Enabled = true;
if (totalPages == 1)
{
btnLast.Enabled = false;
btnNext.Enabled = false;
}
}
else if (this.SmartGridView1.PageIndex == totalPages - 1)
{
btnLast.Enabled = false;
btnNext.Enabled = false;
btnFirst.Enabled = true;
btnPrev.Enabled = true;
}
else
{
btnLast.Enabled = true;
btnFirst.Enabled = true;
btnNext.Enabled = true;
btnPrev.Enabled = true;
}
BindData();
}

ページフロント:
コード#コード#
 
  





<br/> <br/> <br/><script type="text/javascript"> <br>function validate() { <br>var gocount=document.getElementById("goCount").value; <br>if(gocount=="") <br>{ <br>alert(" "); <br>return false; <br>} <br>var result; <br>if(isNaN(gocount)) <br>{ <br>alert(" "); <br>document.getElementById("goCount").value=""; <br>return false; <br>} <br>if(parseInt(gocount)<=0|| parseInt(gocount)><%=this.totalPages %>) <br>{ <br>alert(" "); <br>document.getElementById("goCount").value=""; <br>return false; <br>} <br>return true; <br>} <br></script> <br/><form id="form1" runat="server"> <br/><div> <br/><table cellspacing="0" cellpadding="0" width="98%" align="center" border="0"> <br/><tr id="TrGrid" runat="server"> <br/><td align="left"> <br/>○ :<label id="lblpage" runat="server"/>/ <br/><label id="lblpagesum" runat="server"/>, :<label>ID="lblrowscount" runat="server"></label> </td> <br/><td align="right"> <br/><linkbutton id="btnFirst" runat="server" oncommand="NavigateToPage">CommandArgument="First" CommandName="Pager" Text=" ">[ ]</linkbutton><linkbutton>ID="btnPrev" runat="server" CommandArgument="Prev" OnCommand="NavigateToPage" <br/>CommandName="Pager" Text=" ">[ ]</linkbutton><linkbutton id="btnNext">runat="server" CommandArgument="Next" OnCommand="NavigateToPage" <br/>CommandName="Pager" Text=" ">[ ]</linkbutton><linkbutton id="btnLast">runat="server" CommandArgument="Last" OnCommand="NavigateToPage" <br/>CommandName="Pager" Text=" " >[ ]</linkbutton> <br/><textbox id="goCount" runat="server" width="50px"/> <br/><button id="gobtn" runat="server" text="go" onclientclick="return validate();" onclick="gobtnClick"/></td> <br/></tr> <br/></table> <br/><table cellspacing="0" cellpadding="0" width="98%" align="center" border="0"> <br/><tr> <br/><td> <br/><smartgridview id="SmartGridView1" skinid="blue" runat="server">PagingStyle="Default" <br/>CssClassMouseOver="grid_over" EnableViewState="False" <br/>AutoGenerateColumns="False" <br/>Width="100%" currentLanguage=""> <br/><columns> <br/><templatefield> <br/><headertemplate> <br/><checkbox id="HeadCheckBox" runat="server"/> <br/></headertemplate> <br/><itemtemplate> <br/><checkbox id="checkitem" runat="server" __designer:wfdid="w1"/> <br/></itemtemplate> <br/><headerstyle cssclass="tdcell"/> <br/><itemstyle horizontalalign="Center" width="5px" cssclass="tdcell"/> <br/></templatefield> <br/><boundfield datafield="USER_Account" headertext=" " sortexpression="USER_Account"> <br/><headerstyle cssclass="tdcell"/> <br/><itemstyle horizontalalign="Center" cssclass="tdcell"/> <br/></boundfield> <br/><boundfield datafield="USER_DspName" headertext=" " sortexpression="USER_DspName"> <br/><headerstyle cssclass="tdcell"/> <br/><itemstyle horizontalalign="Center" cssclass="tdcell"/> <br/></boundfield> <br/><boundfield datafield="USER_DspEngName" headertext=" " sortexpression="USER_DspEngName"> <br/><headerstyle cssclass="tdcell"/> <br/><itemstyle cssclass="tdcell" horizontalalign="Center"/> <br/></boundfield> <br/><boundfield datafield="USER_HRID" headertext=" " sortexpression="USER_HRID"> <br/><headerstyle cssclass="tdcell"/> <br/><itemstyle cssclass="tdcell" horizontalalign="Center"/> <br/></boundfield> <br/><boundfield datafield="USER_Email" headertext=" " sortexpression="USER_Email"> <br/><headerstyle cssclass="tdcell"/> <br/><itemstyle horizontalalign="Center" cssclass="tdcell"/> <br/></boundfield> <br/><boundfield datafield="USER_Title" headertext=" " sortexpression="USER_Title"> <br/><headerstyle cssclass="tdcell"/> <br/><itemstyle horizontalalign="Center" cssclass="tdcell"/> <br/></boundfield> <br/><boundfield datafield="USER_Tel" headertext=" " sortexpression="USER_Tel"> <br/><headerstyle cssclass="tdcell"/> <br/><itemstyle horizontalalign="Center" cssclass="tdcell"/> <br/></boundfield> <br/><boundfield datafield="USER_Active" headertext=" " sortexpression="USER_Active"> <br/><headerstyle cssclass="tdcell"/> <br/><itemstyle horizontalalign="Center" cssclass="tdcell"/> <br/></boundfield> <br/><boundfield datafield="USER_Major" headertext=" " sortexpression="USER_Major"> <br/><headerstyle cssclass="tdcell" horizontalalign="Center"/> <br/><itemstyle cssclass="tdcell" horizontalalign="Center"/> <br/></boundfield> <br/></columns> <br/><headerstyle cssclass="thHead"/> <br/><fixrowcol tableheight="" tablewidth=""/> <br/><rowstyle cssclass="tdcell"/> <br/></smartgridview> <br/></td></tr> <br/></table> <br/></div> <br/></form> <br/> <br/> <br/> </code></pre> <div class="clearfix"> <span id="art_bot" class="jbTestPos"/> </div> </div> </div> </div>