asp.NetRepeaterの非常に良いデータのページ分け
95707 ワード
ページングコントロールのソースコードは次のとおりです.
ベースクラスRepeater.csコードは以下の通りです.
使用するストレージ・プロシージャは次のとおりです.
呼び出されたデータベースの最下位コード:
フロントページの例:
バックグラウンドコード:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
#region Assembly Resource Attribute
[assembly: System.Web.UI.WebResource("WebControlLibrary.Js.AjaxHelper.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("WebControlLibrary.img.bg_pager.png", "image/gif")]
#endregion
namespace WebControlLibrary
{
///
/// Repeater Ajax ( Ajax )
///
[DefaultProperty("Text")]
[ToolboxData("{0}:RepeaterPages>")]
public class RepeaterPages : WebControlLibrary.Repeater
{
//
///
///
public RepeaterPages()
: base()
{
}
///
///
///
private string _NextPage = ">";
///
///
///
[Bindable(false), Description(" ")]
public string NextPage
{
get { return _NextPage; }
set { _NextPage = value; }
}
///
///
///
private string _LastPage = "
///
///
///
[Bindable(false), Description(" ")]
public string LastPage
{
get { return _LastPage; }
set { _LastPage = value; }
}
///
///
///
private int _RecordCount;
///
///
///
private int _PageSize;
///
///
///
private int _CurrentPage;
///
///
///
private int _AllPages;
///
///
///
private string _Url;
///
/// Sql
///
private string _SqlString;
///
///
///
private int _PageNumberCount;
///
///
///
private string _ShowTableCollName;
///
///
///
[Bindable(true), Category("Appearance"), DefaultValue("0")]
private int RecordCount
{
get
{
int IntTempValue = 0;
if (Int32.TryParse(Sql_Function.SqlDb.RecordCounts(SqlString), out IntTempValue))
return IntTempValue;
else
return 0;
}
set
{
_RecordCount = value;
}
}
///
///
///
[Bindable(true), Category("Appearance"), DefaultValue("50"), Description(" ")]
public int PageSize
{
get
{
if (_PageSize == 0 || _PageSize.ToString() == "" || _PageSize > 200)
{
return 50;
}
else if (Sql_Function.PublicFunction.isNumber(_PageSize.ToString()))
{
return 50;
}
else if (_PageSize <= 0)
{
return 50;
}
else if (_PageSize > 200)
{
return 50;
}
else
{
return _PageSize;
}
}
set { _PageSize = value; }
}
///
///
///
[Bindable(true), Category("Appearance"), DefaultValue("5"), Description(" ")]
public int PageNumberCount
{
get
{
if (_PageNumberCount == 0 || _PageNumberCount.ToString() == "" || _PageNumberCount > 30)
{
return 5;
}
else if (Sql_Function.PublicFunction.isNumber(_PageNumberCount.ToString()))
{
return 5;
}
else if (_PageNumberCount <= 0)
{
return 5;
}
else if (_PageNumberCount > 30)
{
return 5;
}
else
{
return _PageNumberCount;
}
}
set { _PageNumberCount = value; }
}
///
///
///
private int AllPages
{
get
{
int totalpage;
if (RecordCount / PageSize == 0)
{
totalpage = RecordCount / PageSize;
}
else
{
totalpage = RecordCount / PageSize + 1;
}
return totalpage;
}
set { _AllPages = value; }
}
///
///
///
private int CurrentPage
{
get
{
string RequestCurrentPage = System.Web.HttpContext.Current.Request["page"];
#region
int result_currentpage = 1;
if (RequestCurrentPage == null)/// PAGE
{
result_currentpage = 1;
}
if (RequestCurrentPage != null)
{
if (RequestCurrentPage.Length > 10 || RequestCurrentPage.Length < 1)/// Convert.ToInt32 page=
{
result_currentpage = 1;
}
else///
{
if (Sql_Function.PublicFunction.isNumber(RequestCurrentPage))//
{
result_currentpage = 1;
}
else
{
if (Convert.ToInt32(RequestCurrentPage) > AllPages)///
{
result_currentpage = 1;
}
else
{
if (Convert.ToInt32(RequestCurrentPage) <= 1)/// 1
{
result_currentpage = 1;
}
else
{
result_currentpage = Convert.ToInt32(RequestCurrentPage);
}
}
}
}
}
else
{
result_currentpage = 1;
}
return result_currentpage;
#endregion
}
set { _CurrentPage = value; }
}
///
/// URL ( URL )
///
[Bindable(false), Description(" URL ( URL )")]
public string Url
{
get
{
if (_Url == null || _Url.ToString() == "")
{
return "?";
}
else
{
if (_Url.IndexOf("?") == -1)
{
string a = "?";
a = a + _Url;
return a;
}
else if (Convert.ToInt32(StringFindCount(_Url, "?")) > 1)// ?
{
return "?";
}
else
{
return _Url;
}
}
}
set { _Url = value; }
}
///
/// Sql
///
[Bindable(false), Description(" Sql :select count(id) from tablename")]
public string SqlString
{
get
{
if (_SqlString == null || _SqlString.ToString() == "" || _SqlString.Length < 15)
{
return "";
}
else
{
return _SqlString;
}
}
set { _SqlString = value; }
}
///
///
///
[Bindable(false), Description(" ")]
public string ShowTableCollName
{
get
{
if (_ShowTableCollName == null || _ShowTableCollName == "")
{
return "*";
}
else
{
return _ShowTableCollName;
}
}
set { _ShowTableCollName = value; }
}
///
/// HTML
///
private string _PageBeforeHtml;
///
/// HTML
///
private string _PageLastHtml;
///
/// HTML
///
[Bindable(false), Description(" HTML ")]
public string PageBeforeHtml
{
get { return _PageBeforeHtml; }
set { _PageBeforeHtml = value; }
}
///
/// HTML
///
[Bindable(false), Description(" HTML ")]
public string PageLastHtml
{
get { return _PageLastHtml; }
set { _PageLastHtml = value; }
}
///
///
///
private string _Css = "";
///
///
///
[Bindable(false), Category("Behavior"), DefaultValue(" "), TypeConverter(typeof(WebPageCssSelect)), Description(" ")]
public string Css
{
get { return _Css; }
set { _Css = value; }
}
///
///
///
private bool _SelectPage = true;
///
///
///
[Bindable(false), Description(" ")]
public bool SelectPage
{
get { return _SelectPage; }
set { _SelectPage = value; }
}
///
///
///
private string _AlignSorts = "right";
///
///
///
[Bindable(false), DefaultValue("right"), TypeConverter(typeof(WebPageAlign)), Description(" ")]
public string AlignSorts
{
get { return _AlignSorts; }
set { _AlignSorts = value; }
}
///
/// Js
///
private string _JsFunctionName = "_doPostBack";
///
/// Js
///
[Bindable(false), Description(" Js ")]
public string JsFunctionName
{
get
{
if (_JsFunctionName == null || _JsFunctionName.Replace("'", "").Replace("\"", "") == "")
{
return "_doPostBack";
}
else
{
return _JsFunctionName;
}
}
set { _JsFunctionName = value; }
}
///
///
///
private bool _AutoPageConcise = false;
///
///
///
[Bindable(false), Description(" ")]
public bool AutoPageConcise
{
set { _AutoPageConcise = value; }
get { return _AutoPageConcise; }
}
///
///
///
private bool _BoolIsAutoAjaxPage = false;
///
///
///
[Bindable(false), Description(" ")]
public bool BoolIsAutoAjaxPage
{
get { return _BoolIsAutoAjaxPage; }
set { _BoolIsAutoAjaxPage = value; }
}
///
/// ascx
///
private string _AutoAjaxPageUrl = "";
///
/// ascx
///
[Bindable(false), Description(" ascx ")]
public string AutoAjaxPageUrl
{
get { return _AutoAjaxPageUrl; }
set { _AutoAjaxPageUrl = value; }
}
///
///
///
private string _AutoAjaxReturnPageUrl = "";
///
///
///
[Bindable(false), Description(" ")]
public string AutoAjaxReturnPageUrl
{
get { return _AutoAjaxReturnPageUrl; }
set { _AutoAjaxReturnPageUrl = value; }
}
///
/// ID
///
private string _AutoAjaxPageWebControl = "";
///
/// ID
///
[Bindable(false), Description(" ID")]
public string AutoAjaxPageWebControl
{
get { return _AutoAjaxPageWebControl; }
set { _AutoAjaxPageWebControl = value; }
}
///
/// html,
///
/// HTML
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
if (!this.Page.ClientScript.IsClientScriptBlockRegistered(this.Page.GetType(), "AjaxPageJs"))
{
output.Write("
var AjaxUrl='" + AutoAjaxReturnPageUrl + "'; ");
output.Write("
");
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "AjaxPageJs", "");
}
#region
string CssInfo = WebPageCss.CssStyle(Css, AlignSorts, this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControlLibrary.img.bg_pager.png"));
output.WriteLine(CssInfo);
#endregion
#region Js
output.WriteLine("
");
<br>
<br>output.WriteLine(" function " + JsFunctionName + "(Obj,PageIndex)
");
<br>
<br>output.WriteLine(" {
");
<br>
<br>if (BoolIsAutoAjaxPage)
<br>{
<br>output.WriteLine(" AjaxHelper.Updater('" + AutoAjaxPageUrl.Replace("'", "").Replace("\"", "") + "','" + AutoAjaxPageWebControl + "','" + System.Web.HttpContext.Current.Request.ServerVariables["QUERY_STRING"] + "&page=' + PageIndex + '');
");
<br>
<br>}
<br>else
<br>{
<br>output.WriteLine(" var getUrl=\"" + Url.Replace("'", "").Replace("\"", "") + "&page=\" + PageIndex + \"\";
");
<br>
<br>output.WriteLine(" document.location.href=getUrl;
");
<br>}
<br>
<br>output.WriteLine(" }
");
<br>
<br>output.WriteLine("
");
#endregion
if (!DesignMode)
{
string Result = Pagination();
if (RecordCount > PageSize)
{
output.WriteLine(Result);
}
}
}
///
///
///
/// true:
///
///
/// ( ,
/// order by
/// Where
///
public System.Data.DataTable getObjectRecordCount(bool IsProc, string PorcedureName, string TableName, string TableCollName, string OrderBy, string Where)
{
System.Data.DataTable DataTables = new System.Data.DataTable();
if (IsProc)
{
DataTables = Sql_Function.SqlDb.ReturnDataTable("" + PorcedureName + " '" + TableName + "','" + TableCollName + "'," + CurrentPage + "," + PageSize + ",'" + ShowTableCollName.Replace("'", "") + "','" + OrderBy + "','" + Where + "'");
}
else//
{
string StrSql = string.Format("select {0} from {1} where {2} is not null", ShowTableCollName, TableName, TableCollName);
if (Where != null && Where.Trim().Replace("'", "") != "")
{
StrSql += " and " + Where.Trim().Replace("'", "");
}
if (OrderBy != null && OrderBy.Trim().Replace("'", "") != "")
{
StrSql += " order by " + OrderBy.Trim().Replace("'", "");
}
try
{
DataTables = Sql_Function.SqlDb.ReturnDataTablePage(StrSql, CurrentPage, PageSize, "ReturnDataTable");
}
catch
{
DataTables = null;
}
}
return DataTables;
}
///
///
///
///
/// Url
///
public string Pagination()
{
int allpage = 0;//
int next = 0;//
int pre = 0;//
int startcount = 0;//
int endcount = 0;//
string pagestr = "";//
if (CurrentPage < 1) { CurrentPage = 1; }
//
if (PageSize != 0)
{
allpage = (RecordCount / PageSize);
allpage = ((RecordCount % PageSize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = CurrentPage + 1;
pre = CurrentPage - 1;
if (PageNumberCount % 2 == 0)
{
startcount = (CurrentPage + PageNumberCount / 2) > allpage ? allpage - (PageNumberCount - 1) : CurrentPage - (PageNumberCount / 2 - 1);
endcount = CurrentPage < PageNumberCount / 2 ? PageNumberCount : CurrentPage + PageNumberCount / 2;
}
else
{
startcount = (CurrentPage + PageNumberCount / 2) > allpage ? allpage - (PageNumberCount - 1) : CurrentPage - (PageNumberCount / 2 - 1);
endcount = CurrentPage < PageNumberCount / 2 ? PageNumberCount : CurrentPage + PageNumberCount / 2 + 1;
}
if (startcount < 1) { startcount = 1; } // , 1 1
if (allpage < endcount) { endcount = allpage; }// +5 ,
pagestr = "
" + PageBeforeHtml + "
";
pagestr += "
";
if (!AutoPageConcise)
{
pagestr += " :" + CurrentPage + "/" + allpage + "
";
pagestr += " " + PageSize.ToString() + "
";
pagestr += " " + RecordCount + "
";
}
#region
//pagestr += CurrentPage > 1 ? "1.. " + LastPage + "" : "" + LastPage + "";
pagestr += CurrentPage > 1 ? "1.. " + LastPage + "" : "" + LastPage + "";
#endregion
#region , ,
//// , ,
for (int i = startcount; i <= endcount; i++)
{
//pagestr += CurrentPage == i ? " " + i + "" : " " + i + "";
pagestr += CurrentPage == i ? "
" + i + "" : "
" + i + "";
}
#endregion
#region
//pagestr += CurrentPage != allpage ? " " + NextPage + " .." + allpage + "" : "" + NextPage + "";
pagestr += CurrentPage != allpage ? " " + NextPage + " .." + allpage + "" : "" + NextPage + "";
#endregion
if (SelectPage)
{
pagestr += "
";
pagestr += "
function PagegetUrls(Values)
{
if (13==event.keyCode)
{
javascript:" + JsFunctionName + "('" + this.ClientID.ToString() + "',Values)
return false;
}
}
";
}
pagestr += "
" + PageLastHtml + "
";
return pagestr;
}
///
///
///
///
///
///
///
private string SelectPages(int PageCount, int CurrentPage, string Url)
{
string ResultPage = "";
for (int k = 1; k <= PageCount; k++)
{
if (k == CurrentPage)
{
ResultPage += "";
}
else
{
ResultPage += "";
}
}
return ResultPage;
}
///
///
///
///
/// , , ,
///
private string StringFindCount(string str, string split)
{
int c1 = 0;
string a = split;
if (split == null || split.ToString() == "" || split.ToString().Trim().Length < 1)
{
a = " ";
}
else if (split.ToString().Trim().Length > 1)
{
a = a.Substring(0, 1);
}
else
{
}
if (str == null || str.ToString().Trim().Replace("'", "") == "" || str.ToString().Trim().Replace("'", "").Length < 1)
{
c1 = 0;
}
else
{
for (int i = 0; i < str.Length; i++)
{
if (str[i] == char.Parse(a))
{
c1++;
}
}
}
return c1.ToString();
}
}
///
///
///
public class WebPageCssSelect : StringConverter
{
///
///
///
public WebPageCssSelect() { }
///
///
///
///
///
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
///
///
///
///
///
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
ArrayList CssArray = new ArrayList();
CssArray.Add("Digg Style");
CssArray.Add("Yahoo Style");
CssArray.Add("New Yahoo! Style");
CssArray.Add("Meneame Style");
CssArray.Add("Flickr Style");
CssArray.Add("Sabros.us Style");
CssArray.Add("Green Style");
CssArray.Add("Gray Style");
CssArray.Add("Black Style");
CssArray.Add("Mis Algoritmos Style");
CssArray.Add("Black-Red Style");
CssArray.Add("Gray Style 2");
CssArray.Add("Yellow Style");
CssArray.Add("jogger Style");
CssArray.Add("512megas Style");
CssArray.Add("Technorati Style");
CssArray.Add("YouTube Style");
CssArray.Add("MSDN Search Style");
CssArray.Add("Msn Gray Style");
CssArray.Add("Badoo");
CssArray.Add("Blue Style");
CssArray.Add("Green-Black Style");
CssArray.Add("viciao Style");
CssArray.Add(" ");
return new StandardValuesCollection(CssArray);
}
///
/// return ture ,return flase
///
///
///
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;
}
}
///
///
///
public class WebPageAlign : StringConverter
{
///
///
///
public WebPageAlign() { }
///
///
///
///
///
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
///
///
///
///
///
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
ArrayList CssArray = new ArrayList();
CssArray.Add("right");
CssArray.Add("left");
CssArray.Add("center");
return new StandardValuesCollection(CssArray);
}
///
/// return ture ,return flase
///
///
///
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;
}
}
///
///
///
public class WebPageCss
{
///
///
///
///
///
///
///
public static string CssStyle(string Css, string AlignSorts,string BackGroundImageUrl)
{
string Result = "";
#region
Result+="
";
#endregion
return Result;
}
}
}
ベースクラスRepeater.csコードは以下の通りです.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary
{
///
/// Repeater 。
///
[DefaultProperty("Text"), ToolboxData("{0}:Repeater>")]
public class Repeater : System.Web.UI.WebControls.Repeater
{
///
///
///
public Repeater()
: base()
{
}
}
}
使用するストレージ・プロシージャは次のとおりです.
------------------------------------
-- :
-- :2009-07-18 22:12
-- :
--qq :42383239
------------------------------------
CREATE PROC PageView
@tbname sysname, --
@FieldKey nvarchar(1000), -- ( ) ,
@PageCurrent int=1, --
@PageSize int=10, -- ( )
@FieldShow nvarchar(1000)='', -- , ,
@FieldOrder nvarchar(1000)='', -- , DESC/ASC
@Where nvarchar(1000)='' --
--@PageCount int=null OUTPUT --
AS
SET NOCOUNT ON
declare @PageCount int
--
IF OBJECT_ID(@tbname) IS NULL
BEGIN
RAISERROR(N' "%s" ',1,16,@tbname)
RETURN
END
IF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0
BEGIN
RAISERROR(N'"%s" 、 ',1,16,@tbname)
RETURN
END
--
IF ISNULL(@FieldKey,N'')=''
BEGIN
RAISERROR(N' ( )',1,16)
RETURN
END
--
IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
IF len(@PageCurrent)>5 SET @PageCurrent=1
IF ISNULL(@PageSize,0)<1 SET @PageSize=10
IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'
IF ISNULL(@FieldOrder,N'')=N''
SET @FieldOrder=N''
ELSE
SET @FieldOrder=N'ORDER BY '+LTRIM(@FieldOrder)
IF ISNULL(@Where,N'')=N''
SET @Where=N''
ELSE
SET @Where=N'WHERE ('+@Where+N')'
-- @PageCount NULL , ( , , , , , @PageCount )
IF @PageCount IS NULL
BEGIN
DECLARE @sql nvarchar(4000)
SET @sql=N'SELECT @PageCount=COUNT(*)'
+N' FROM '+@tbname
+N' '+@Where
--EXEC sp_executesql @sql,N'@PageCount int OUTPUT',@PageCount OUTPUT
SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
END
-- TOPN
DECLARE @TopN varchar(20),@TopN1 varchar(20)
SELECT @TopN=@PageSize,
@TopN1=(@PageCurrent-1)*@PageSize
--
IF @PageCurrent=1
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' '+@Where
+N' '+@FieldOrder)
ELSE
BEGIN
--
IF @FieldShow=N'*'
SET @FieldShow=N'a.*'
-- ( )
DECLARE @Where1 nvarchar(4000),@Where2 nvarchar(4000),
@s nvarchar(1000),@Field sysname
SELECT @Where1=N'',@Where2=N'',@s=@FieldKey
WHILE CHARINDEX(N',',@s)>0
if len(@s)>2----- 2
begin
SELECT @Field=LEFT(@s,CHARINDEX(N',',@s)-1),
@s=STUFF(@s,1,CHARINDEX(N',',@s),N''),
@Where1=@Where1+N' AND a.'+@Field+N'=b.'+@Field,
@Where2=@Where2+N' AND b.'+@Field+N' IS NULL',
@Where=REPLACE(@Where,@Field,N'a.'+@Field),
@FieldOrder=REPLACE(@FieldOrder,@Field,N'a.'+@Field),
@FieldShow=REPLACE(@FieldShow,@Field,N'a.'+@Field)
end
else
begin
SELECT @Field=@s,
@s=STUFF(@s,1,CHARINDEX(N',',@s),N''),
@Where1=@Where1+N' AND a.'+@Field+N'=b.'+@Field,
@Where2=@Where2+N' AND b.'+@Field+N' IS NULL',
@Where=REPLACE(@Where,@Field,N'a.'+@Field),
@FieldOrder=REPLACE(@FieldOrder,@Field,N'a.'+@Field),
@FieldShow=REPLACE(@FieldShow,@Field,N'a.'+@Field)
end
SELECT @Where=REPLACE(@Where,@s,N'a.'+@s),
@FieldOrder=REPLACE(@FieldOrder,@s,N'a.'+@s),
@FieldShow=REPLACE(@FieldShow,@s,N'a.'+@s),
@Where1=STUFF(@Where1+N' AND a.'+@s+N'=b.'+@s,1,5,N''),
@Where2=CASE
WHEN @Where='' THEN N'WHERE ('
ELSE @Where+N' AND ('
END+N'b.'+@s+N' IS NULL'+@Where2+N')'
--
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' a LEFT JOIN(SELECT TOP '+@TopN1
+N' '+@FieldKey
+N' FROM '+@tbname
+N' a '+@Where
+N' '+@FieldOrder
+N')b ON '+@Where1
+N' '+@Where2
+N' '+@FieldOrder)
END
GO
呼び出されたデータベースの最下位コード:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.Data.Common;
using System.Text.RegularExpressions;
using System.Reflection;
using System.IO;
namespace Sql_Function
{
///
///
///
public class SqlDb
{
#region
public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
#endregion
#region
///
///
///
///
public static SqlConnection OpenDataBase()
{
SqlConnection conn = new SqlConnection(ConnectionString);
try
{
conn.Open();
return conn;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
///
///
///
///
public static void CloseDataBase(SqlConnection conn)
{
try
{
if (conn != null)
{
conn.Close();
conn.Dispose();
}
}
catch (Exception ex)
{
conn.Close();
conn.Dispose();
throw new Exception(ex.Message);
//throw new ArgumentNullException("connection");
}
finally
{
conn.Close();
}
}
#endregion
#region DataTable
///
/// Sql DataTable
///
/// Sql
/// DataTable
public static DataTable ReturnDataTable(string strSQL)
{
SqlConnection conn = OpenDataBase();
DataTable table = new DataTable();
SqlDataAdapter da = null;
try
{
da = new SqlDataAdapter(strSQL, conn);
da.Fill(table);
da.Dispose();
CloseDataBase(conn);
return table;
}
catch (Exception e)
{
da.Dispose();
table.Dispose();
CloseDataBase(conn);
throw new Exception(e.Message);
}
finally
{
da.Dispose();
CloseDataBase(conn);
}
}
///
/// DataTable ,
///
/// SQL
///
///
///
///
public static DataTable ReturnDataTablePage(string sqls, int currentpage, int pagesize, string table)
{
SqlConnection conn = OpenDataBase();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqls, conn);
try
{
int startcount;
if (currentpage < 1)
{
startcount = currentpage * pagesize;
}
else
{
startcount = (currentpage - 1) * pagesize;
}
da.Fill(ds, startcount, pagesize, table);
da.Dispose();
CloseDataBase(conn);
return ds.Tables[0];
}
catch (Exception ex)
{
da.Dispose();
CloseDataBase(conn);
ds.Dispose();
throw new Exception(ex.Message);
}
finally
{
da.Dispose();
CloseDataBase(conn);
}
}
#endregion
#region
///
/// count( ) Sql
///
/// count( ) Sql
///
public static string RecordCounts(string StrSql)
{
string ProcInfo = "0";
SqlConnection conn = OpenDataBase();
SqlCommand comm = new SqlCommand(StrSql, conn);
comm.CommandTimeout = 120;
SqlDataReader DataReaders = comm.ExecuteReader(); //
if (DataReaders.Read())
{
ProcInfo = DataReaders[0].ToString();
}
DataReaders.Close();
comm.Dispose();
CloseDataBase(conn);
return ProcInfo;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Sql_Function
{
public class PublicFunction
{
///
/// TRUE ,False
///
///
///
public static bool isNumber(string s)
{
int Flag = 0;
char[] str = s.ToCharArray();
for (int i = 0; i < str.Length; i++)
{
if (Char.IsNumber(str[i]))
{
Flag++;
}
else
{
Flag = -1;
break;
}
}
if (Flag > 0)
{
return false;
}
else
{
return true;
}
}
}
}
フロントページの例:
Repeater
- ・
バックグラウンドコード:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace FrameWork.Web
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void Bind()
{
string Sql = "select count(Sys_NewsList_ID) from Sys_NewsList";// Sql
#region
#region
RepeaterPages1.SqlString = Sql;// Sql
RepeaterPages1.LastPage = "< Prev";
RepeaterPages1.NextPage = "Next > ";
RepeaterPages1.PageBeforeHtml = "";
RepeaterPages1.PageLastHtml = "";
#region Url
System.Text.StringBuilder StrBulider = new System.Text.StringBuilder();
StrBulider.Append("&a=");
#endregion
RepeaterPages1.Url = StrBulider.ToString();
#endregion
#region
RepeaterPages1.DataSource = RepeaterPages1.getObjectRecordCount(true, "PageView", "Sys_NewsList", "Sys_NewsList_ID", " Sys_NewsList_ID desc", " Sys_NewsList_ID is not null");
RepeaterPages1.DataBind();
#endregion
#endregion
}
}
}