2011-08-10 34 views
2

我有一个连接到SQL数据库的listview控件,我已经设置了一个数据限制器来限制每个页面上显示的项目(每页3个)。Datapager控件每5秒自动更改页面

我已经将datapager设置为:visible = false,并且想知道如何每5秒自动创建一次数据打包器更改页面。

在此先感谢您提供的任何帮助。

+0

您将需要使用[计时器控制](http://msdn.microsoft.com/en-us/library/bb386404.aspx)或深入研究一些[Javascript](http://www.mcfedries。 COM /的JavaScript/timer.asp) –

回答

3

我有同样的问题,解决方案非常简单。

第一步是在页面上包含一个DataPager控件和一个Timer控件。

<asp:DataPager ID="pager" runat="server" PagedControlID="listView" PageSize="10"> 
    <Fields> 
     <asp:NumericPagerField ButtonType="Link" /> 
    </Fields> 
</asp:DataPager> 

<asp:Timer ID="timer" runat="server" Interval="1000" OnTick="timer_Tick"> 
</asp:Timer> 

接下来,你必须编写代码:

protected void timer_Tick(object sender, EventArgs e) { 
    //Verify that the session variable is not null 
    if (Session["startRowIndex"] == null) 
     Session.Add("startRowIndex", 0); 
    //Create a variable to store the first record to show 
    int startRowIndex = Convert.ToInt32(Session["startRowIndex"]); 
    //Show from the first record to the size of the page 
    this.pager.SetPageProperties(startRowIndex, this.pager.MaximumRows, true); 
    //Increase the first record to display in the size of the page 
    startRowIndex += this.pager.MaximumRows; 
    //If the first record exceeds the total number of records, restart the count. 
    if (startRowIndex > this.pager.TotalRowCount) startRowIndex = 0; 
     Session["startRowIndex"] = startRowIndex; 
} 

,并把这个代码在Page_Load事件:

protected void Page_Load(object sender, EventArgs e) { 
    //This session variable to control the record to show for each tick 
    if (!IsPostBack) Session.Add("startRowIndex", 0); 
} 

我希望你提供的东西,如果它不是太晚了,并为我的英语感到抱歉,因为它不是我的母语。

来自智利的问候。