本文目的:动态设置DataPager控件中页码的图片。
使用ListView+DataPager进行绑定数据时,遇到的一个问题?
当使用DataPager时,NextPreviousPagerField有三个ButtonType 类型(Button\Image\Link),当使用ButtonType=Image时,会涉及做切换图片的问题?
ps. 为什么要切换图片显示呢?是因为翻页时,如果翻到最后一页时,切换一张无效的图片,让用户感觉到已经不能再点击了。
那在程序中如何动态切换图片呢?
早先,我一直以为使用DataPager自身的事件就可以搞定,玩了半天DataPager----OnDataBinding、OnInit、OnPreRender都是不可以了;
虽然其中OnInit、OnPreRender是可以动态改变图片的,但是在这个事件中无法取当数据的页码值,(就是不知道用户翻到几页了,是最后一页还是第一页)。
其实,实现这个需求也蛮简单的,直接使用ListView中OnDataBinding事件就可以实现动态加载不同状态的图片了。
(说白了DataPager还是围绕着ListView工作的,毕竟DataPager中PageControlID=ListViewID的)
最后
附上代码
Page:
<form id="form1" runat="server">
<asp:ListView ID="ProductList" runat="server" OnDataBinding="ProductList_DataBinding"
OnPagePropertiesChanging="ProductList_PagePropertiesChanging">
<ItemTemplate>
<%#Eval ( "Test" )%>
</ItemTemplate>
</asp:ListView>
<br />
<asp:DataPager ID="dpHotelInfoList" runat="server" PagedControlID="ProductList" PageSize="5">
<Fields>
<asp:NextPreviousPagerField ButtonType="Image" ShowFirstPageButton="true" ShowLastPageButton="true" />
</Fields>
</asp:DataPager>
</form>