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:
therearesomeeggachequestionhere
1.insysobjects
so,dropatemptableis
2.execavariableislikethis,don’tforgetthebrackets.
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是否正确
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
S | SYSTEMTABLE |
V | VIEW |
U | USERTABLE(包括临时表) |
TR | TRIGGER |
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是否正确
相关文章推荐
- Developing ASP.NET Custom Control With C# Builder
- Redirecting to custom 401 page when "Access denied" occures within an ASP.NET application with Windows authentication
- Developing ASP.NET Custom Control With C# Builder
- Developing ASP.NET Custom Control With C# Builder
- Developing ASP.NET Custom Control With C# Builder
- Developing ASP.NET Custom Control With C# Builder
- 【转】ASP.NET 数据分页第三篇 - 结合 Custom Control 处理 GridView 的 UI 呈现
- ASP.NET自定义控件示例:ASP.NET Custom control with designer integration
- Using jQuery with the ASP.NET CustomValidator Control(转)
- ASP.NET: Custom AutoCompleteTextBox WebControl [With Source Code]
- Developing ASP.NET Custom Control With C# Builder
- Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application
- Developing ASP.NET Custom Control With C# Builder
- Developing ASP.NET Custom Control With C# Builder
- asp.net分页,Effective paging with List View control in ASP.NET
- Working with Data in ASP.NET 2.0 :: Using TemplateFields in the GridView Control
- Developing ASP.NET Custom Control With C# Builder
- Login control in an ASP.NET AJAX toolkit PopupControlExtender with a close button--write by Laurent Kempé
- Developing ASP.NET Custom Control With C# Builder
- Developing ASP.NET Custom Control With C# Builder