HiddenField控件就是隐藏输入框的服务器控件,他能让你保存那些不必显示在页面上的且对安全性需求不高的数据。也许这个时候应该有这么一个疑问,为什么有了ViewState、Session和Cookie等状态保存机制,还需要用起HiddenField呢?


增加HiddenField,其实是为了让整个状态管理机制的应用程度更加全方面。因为不管是ViewState、Cookie还是Session,都有其失效的时候,比如用户因某种需求设置ViewState为false,或环境条件限制使用Cookie,或用户长时间没有动作导致Session过期等等,那这个时候HiddenField无疑是最佳选择。

一般可以用于排序方式的选择:

如前台代码:

<webdiyer:AspNetPagerID="AspNetPager1"runat="server"Width="100%"ShowPageIndexBox="Always"PageIndexBoxType="DropDownList"TextBeforePageIndexBox="转到:"HorizontalAlign="Center"PageSize="20"AlwaysShow="True"OnPageChanged="AspNetPager1PageChanged"CssClass="pagination"CurrentPageButtonClass="active"ShowCustomInfoSection="Right"CustomInfoHTML="当前第%CurrentPageIndex%/%PageCount%页共%RecordCount%条记录每页%PageSize%条"></webdiyer:AspNetPager><asp:HiddenFieldrunat="server"ID="SortType"/><asp:HiddenFieldrunat="server"ID="SortField"/>

后台加载数据的部分代码:

privatevoidLoadData(){intcount;intpagesize=AspNetPager1.PageSize;varpageIndex=AspNetPager1.CurrentPageIndex;stringorder="CreatedOn";//排序选择if(!string.IsNullOrEmpty(SortField.Value))//SortField为隐藏控件{order=SortField.Value;}if(SortType.Value=="asc")//SortOrder为隐藏控件,正序{_sortOrder=SortOrder.Ascending;}varlist=_chemicalAdapter.FetchChemicals(_Name.Text,_Number.Text,null,pageIndex,pagesize,order,_sortOrder,outcount);AspNetPager1.RecordCount=count;_ProjectGrid.DataSource=list;_ProjectGrid.DataBind();}