您的位置:首页 > 编程语言 > ASP

An ASP.NET Gridview Control With Custom Paging (Technical)

2012-03-02 15:28 225 查看
Iwasworkingonawebsiteandneededawaytodisplaymyloginformationinagridviewcontrolbutallowpagingthroughacertainnumberofarticlesatatimesothatiftheuserwasviewingthecompletearchive,thepagewouldn'tbetoolong.ThedefaultpagingintheASP.NETgridviewcontroldoesnotscalewellasitalwaysretrievesalltherecordsfromthedatabaseandthenonlydisplaystheonesyouwant.IwantedtoratherfetchonlytherecordsthatIneededforthecurrentpageandletSQLsoalltheworksothattheimpactonmywebapplicationwasless.AftersomesearchingonthewebIfoundvariousarticlesbutnothingthatreallyworkedformesoIdecidedtowritemyown.Incidentally,IstartedbydrawingthelogicoutonapieceofpaperbeforejustdivingintothecodeandthisreallyhelpedwithmyfocusandmadesurethatIdidn'twastetimegoinginthewrongdirection.Now,let'sgettothecode...Firstly,thestructureofmySQLNewsArticletableisasfollows:

ThelogIDisanauto-incrementingidentitycolumn.Soinadditiontothepaging,IneededtobeabletofiltermyrecordsbylogTable/logOper/logTimeaswellassearchword(s)foundintheTitleorBodyfields.Todothis,Icreatedastoredprocedurethatlookslikethis:
IFOBJECT_ID(N'sp_GetLogs',N'P')ISNOTNULL
DROPPROCsp_GetLogs
go
CREATEPROCsp_GetLogs
@iPageIndexINT,
@iMaxRowsINT,
@searchvarchar(1000)
AS
BEGIN
SETNOCOUNTON;
DECLARE@iStartINT
SELECT@iStart=(@iPageIndex-1)*@iMaxRows
DECLARE@iEndINT
SELECT@iEnd=@iStart+@iMaxRows
IFOBJECT_ID(N'#TempLogs',N'U')ISNOTNULL
DROPTABLE#TempLogs
CREATETABLE#TempLogs(
intUniqueIDINTPRIMARYKEYIDENTITY(1,1),
userCNameNVARCHAR(100),
logTableVARCHAR(50),
logOperVARCHAR(50),
logTimeVARCHAR(50),
logContentNVARCHAR(300))
DECLARE@SQLSTRVARCHAR(1000)
SET@SQLSTR='INSERT#TempLogsSELECTU.userCName,S.logTable,S.logOper,S.logTime,S.logContent
FROMSys_LogS
INNERJOINJC_UserinfoUONU.userID=S.logUser
WHERE'+@search;
PRINT(@SQLSTR)
EXEC(@SQLSTR)
SELECT*FROM#TempLogs
WHEREintUniqueID>@iStart
ANDintUniqueID<=@iEnd
END
GO
EXECsp_GetLogs2,5,'LOGTABLE=''JC_DEPARTMENT'''

therearesomeeggachequestionhere
1.insysobjects

SSYSTEMTABLE
VVIEW
UUSERTABLE(包括临时表)
TRTRIGGER
so,dropatemptableis

IFOBJECT_ID(N'#TempLogs',N'U')ISNOTNULL
DROPTABLE#TempLogs

2.execavariableislikethis,don’tforgetthebrackets.

DECLARE@SQLSTRVARCHAR(1000)
SET@SQLSTR='INSERT#TempLogsSELECTU.userCName,S.logTable,S.logOper,S.logTime,S.logContent
FROMSys_LogS
INNERJOINJC_UserinfoUONU.userID=S.logUser
WHERE'+@search;
PRINT(@SQLSTR)
EXEC(@SQLSTR)

TheparamtersIpassintotheprocdureare:

@iPageIndex
@iMaxRows
@search

The@PageIndexreferstothecurrentpageofresultsthatweareviewing.
The@iMaxRowsreferstothenumberofrecordsdisplayedoneachpage-notethatitiscalledmaxrowsbecausethelastpagemaycontainlessthantheothersdependingonthetotalnumberofrecords.
The@searchreferstoanysearchword(s)usedtofiltertheresultsandisalsooptional.
Thefirstthingwedoissetthestartandendvaluesforourrecordsetbasedonthecurrentpageandthenumberofrecordstofetch.Wewillusetheselatertoselectonlytherelevantrecordstopassbacktoourwebapplication.
Nextwecreateatemprorarytablewithanauto-incrementingidentitycolumn-thisisimportantasifrecordsaredeletedfromouroriginalnewsarticletable,theid'swillnolongerbesequential-if,however,weinsertourrecordsintoourtemporarytable,theid'swillbesequentialandwecanselectbetweenourstartandendvalues.
Wethenselecttheappropriaterecordsfromourlogtableandinserttheseintoourtemporarytableandfinally,weselectourresultsfromourtemporarytablewheretheidsarebetweenourstartandendvalues.Afterselectingthem,wedeleteourtemporarytablefromthememory.
So,nowwehaveaSQLstoredprocedurethatwillreturnasetofresultsbasedonsomefilteringandpagingparameters.Howdoweusethisinourwebapplication?
Here'swhatthesourceformyNews.aspxpagelookslike:
------------------------------------------------
------------------------------------------------
NotethatIhavetwo"divs,"oneforthegridviewcontrolandanotheronecalled"Pager"thatwilldisplaythepagenumbersforpaging.(Asanaside-checkthedtPostedcolumn,youwillseethatIhavespecifiedadateformatANDHtmlEncode-iftheHtmlEncodeisnotsettofalse,yourformattingwillnotbeapplied)
Nowthatwehaveourgrid,let'sbindthedatatoitinourcodebehindpage.
InthePage_LoadeventIcallthefollowingroutine:
FillNewsGrid("News.aspx",gvNews,Pager,iMaxRows,iCurrentPage,iCategory,txtSearchText.Value)
Thisroutinelookslikethis:
------------------------------------------------
------------------------------------------------
Lastly,IcompilethelinktoviewthecompletearticleintheRowDataBoundevent:
Therewego,thatshouldgetyoustarted,pleasefeelfreetocontactmeifyouneedanyassistancegettingthisupandrunningonyoursite.
提示:
写存储过程中,可以使用print(@sql)来看下最后的sql是否正确
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: