Using Ajax update panel

So here's how I did it:
 
I have a DisplayItem.aspx page. I have added a "ScriptManager" control and an "Update Panel" control to this page. Now, add a "Repeater" and a "Timer" control to the Update panel. Configure the "Repeater" to use a SqlDataSource as it's datasource.
 
In the select command, I have selected the Top 20 Product IDs and their names from the [Product] table in SQL Server 2005 "AdventureWorks" database.
 
Add a Header template for the two columns we are binding to in the repeater. And add Two label controls inside the ItemTemplate section for the actual data items.
 
Next, I have added an event handler for the Timer's OnTick event and specified 5 seconds as the Timer interval.
 
Now, add a "AsyncPostBackTrigger" to the Triggers collection of the update panel. Give the ID of the Timer control in the 'ControlID' property and use "Tick" for the EventName.

<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager>

<div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> <HeaderTemplate> <table> <tr> <td>ProductID</td> <td>Product Name</td> </tr> </table> </HeaderTemplate> <ItemTemplate> <table> <tr> <td>

<%

# DataBinder.Eval(Container.DataItem, "PK_Product") %></td> <td><%# DataBinder.Eval(Container.DataItem, "Product_Name") %></td> </tr> </table> </ItemTemplate> </asp:Repeater> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>" SelectCommand="Select top 20 [PK_Product], [Product_Name] from MSSQLTIPS_SSAS_1.[Product]"> </asp:SqlDataSource> <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_OnTick" Interval="5000" > </asp:Timer> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> </Triggers> </asp:UpdatePanel>

 
Next, in the 'OnTick' event handler, I have added a simple logic to check if the interval has elapsed a certain timeperiod, and if not, then to retrieve the next set of 20 Products from the database and bind it to the repeater.
 
int Timer = 1;

if (Session["Timer"] == null){

   Session["Timer"] = Timer;

   Session["StartIndx"] = 1;

   Session["EndIndx"] = 20;

}

else

{

  if (Convert.ToInt32(Session["Timer"]) > 4)

      Session.Remove("Timer");

  else

  {

    Session[

"Timer"] = Convert.ToInt32(Session["Timer"]) + 1;

    Session["StartIndx"] = Convert.ToInt32(Session["StartIndx"]) + 20;

    Session["EndIndx"] = Convert.ToInt32(Session["EndIndx"]) + 21;

   }

}

SqlDataSource1.SelectCommand =

"Select top 20 [PK_Product], [Product_Name] from MSSQLTIPS_SSAS_1.[Product] where [PK_Product] between " + Convert.ToInt32(Session["StartIndx"]) + " and " + Convert.ToInt32(Session["EndIndx"]);

;

   

 

Ebook Download
View all
Learn
View all