Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I cycle through and display text from a set of records, one record at a time at a set interval (i.e. news pager)?

Posted on 2011-03-25
9
Medium Priority
?
311 Views
Last Modified: 2012-05-11
I am trying to display alerts, one record at a time from a database, that cycle to the next or first record at a set interval. I am essentially trying to have a slide show, but for text only.

I am using a similar solution for displaying images with the slideshowextender, but need the effect for text only. This is my current image solution:
http://forums.asp.net/p/1083830/1608950.aspx#1608950

I would also be open to this kind of solution, again, if it worked for text...
http://www.dotnetcurry.com/ShowArticle.aspx?ID=202

Help help!
0
Comment
Question by:devo00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 5

Expert Comment

by:aternex
ID: 35220263
Are you ok with pulling the next alert from a cached source for a specified time period? For example, you could load all of your results into a list which is cached with an expiration of lets say 5 minutes. Then on your actual web page you could then just refresh an updatepanel every 30 seconds or so and loop through the cached list. This would reduce your calls to the database significantly and wouldn't be too complex to implement. The downside with this is that if a new alarm was added, it wouldn't show up on your alert list until the next time the cache was refreshed (unless you used sql dependencies, of course).
0
 

Author Comment

by:devo00
ID: 35220434
Hi aternex, that's fine, but I still need to know how to implement a cycling text alert dataset first and worry about database hits second.
0
 
LVL 5

Accepted Solution

by:
aternex earned 1000 total points
ID: 35220631
Fair enough, here is an example. I'm sure there are a couple of other ways to do this, but this one was pretty easy.

Here is a generic web page that shows the cached object being looped through

'Default.aspx.vb
Imports System.Web.Caching

Public Class _Default
  Inherits System.Web.UI.Page

  Dim alerts As List(Of Alert)

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If alerts Is Nothing Then
      alerts = Cache("Alerts")
    End If

    Dim currentItem As Integer = alerts.IndexOf(lblTest.Text)

    If currentItem >= 0 AndAlso currentItem < alerts.Count - 1 Then
      lblTest.Text = alerts(currentItem + 1)
    Else
      lblTest.Text = alerts(0)
    End If
  End Sub
End Class

'Default.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />

    <div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
        <asp:Label ID="lblTest" runat="server" />
        <asp:Timer ID="Timer1" runat="server" Enabled="true" Interval="5000" />
      </ContentTemplate>
      <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
      </Triggers>
    </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Open in new window


In one application I developed, I had to ensure that nearly all foreign key data was always available. To accomplish this, I used the code pattern below to ensure data was cached and as current as the business demanded. This would show how you could populate your list and have it expire

Imports System.Configuration.ConfigurationManager
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Caching

Public Class CacheHelper
  Private onRemove As CacheItemRemovedCallback = Nothing

  Public Sub New()
    onRemove = New CacheItemRemovedCallback(AddressOf CacheReset)
  End Sub

  ' Fills the cache object with list of alerts
  ' duration is expected to be the number of minutes the cache is valid for
  ' I just put in a simple alert class, so obviously you would need to
  ' modify this to fit your requirements
  Public Sub PopulateCache(ByVal duration As Integer)
    Dim Alerts As New List(Of Alert)

    Using oConn As New SqlConnection(ConnectionStrings("MyDatabase").ConnectionString)
      Using oCmd As SqlCommand = oConn.CreateCommand()
        oCmd.CommandType = CommandType.StoredProcedure
        oCmd.CommandText = "dbo.GetOpenAlerts"

        oCmd.Connection.Open()

        Using oRs As SqlDataReader = oCmd.ExecuteReader(CommandBehavior.CloseConnection)
          While oRs.Read()
            Dim item As New Alert

            item.ID = oRs("ID")
            item.Message = oRs("Message")
            item.CreateDate = oRs("CreateDate")

            Alerts.Add(item)

            oRs.Close()
          End While
        End Using
      End Using
    End Using

    ' The reason I am using insert instead of add is that insert replaces the
    ' cache object if it already exists.
    System.Web.HttpRuntime.Cache.Insert("Alerts", Alerts, Nothing, DateTime.UtcNow.AddMinutes(duration), Cache.NoSlidingExpiration, CacheItemPriority.Normal, onRemove)
  End Sub

  ' The purpose of this routine is to reinsert the object that was just removed
  ' from the cache quickly for a short period of time. The duration should be
  ' long enough to cover the time required for your database query to
  ' return the results. This ensures that data is always available to 
  ' people requesting it as quickly as possible, but won't let the data
  ' become to stale.
  Public Sub ReloadCache(ByVal key As String, ByVal value As Object)
    System.Web.HttpRuntime.Cache.Insert(key, value, Nothing, DateTime.UtcNow.AddMinutes(1), Cache.NoSlidingExpiration, CacheItemPriority.Normal, onRemove)
  End Sub

  ' Once an object is removed, the cache needs to be recreated -- unless
  ' it was purposely removed or hasn't been used often. This fit the 
  ' requirements that I had to meet, but you can change if you need.
  Public Sub CacheReset(ByVal key As String, ByVal value As Object, ByVal reason As CacheItemRemovedReason)
    If reason = CacheItemRemovedReason.Removed OrElse reason = CacheItemRemovedReason.Underused Then
      Exit Sub
    End If

    ' Put the object that was just removed back into the cache
    ReloadCache(key, value)

    ' Recreate the cache with the new object
    PopulateCache(15)
  End Sub
End Class

Open in new window


Then in the global.asax.

  Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim helper As New CacheHelper
    helper.LoadNewCache(15)
    
    helper = Nothing
  End Sub

Open in new window


I hope this is somewhat helpful. Hopefully I didn't make any typing errors.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:aternex
ID: 35220668
For the sake of argument, change it from a List(Of Alert) to a List(Of String) and this example will work. Also, since you mentioned a dataset, you might look into the select method of the underlying datatable to find the row index and use it to loop through to the next one. Again, as with everything in .net there are tons of ways to accomplish the same thing. Sorry if I've provided more confusion than assistance.
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 1000 total points
ID: 35220687
I think that you can use the JQuery library to accomplish this, do you use it in your project? it is very light and I found it very usefull in my projects, here is a small example that I think you want to do:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Rotate Text Example</title>
</head>
<body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">
        var textToDisplay = ['Hello', 'World', 'This', 'Is', 'A', 'Test!'];

        function startRotate(arr) {
            var tc = $('#textContainer');

            if ($.isArray(arr)) {
                tc.stop(true).fadeTo('1', 1).fadeOut('slow', function () {
                    textToDisplay = arr;
                    startRotate();
                });
                return;
            }

            for (var i in textToDisplay) {
                (function (i) {
                    tc.fadeOut(1,
                        function () {
                            document.getElementById('textContainer').innerHTML = textToDisplay[i];
                        }).fadeIn('slow').delay(1000).fadeOut('slow',
                        function () {
                            if (i == textToDisplay.length - 1)
                                startRotate();
                        });
                })(i)
            }
        }

        $(document).ready(function () {
            startRotate();
        });

        function updateText() {
            startRotate(['Breaking New\'s!!!', 'This Is...', 'A New...', 'Test...']);
        }
    </script>
    <h1 id="textContainer"></h1>
    <br />
    <button onclick="updateText()">Update Text</button>
</body>
</html>

Open in new window


Then you can make a Ajax response to the page for update the text using the "startRotate" function and passing an array of strings.

I tested it in Chrome, FF and IE9 and works fine.

I hope it is what you need to solve your problem.
0
 

Author Comment

by:devo00
ID: 35283026
aternex, I'm having trouble converting this to c#, particularly here:
onRemove = New CacheItemRemovedCallback(AddressOf CacheReset)

Do you happen to have this in c#?
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35286705
If you allow me...

System.Web.Caching.CacheItemRemovedCallback onRemove = new System.Web.Caching.CacheItemRemovedCallback(CacheReset);

Open in new window


void CacheReset(string key, object value, System.Web.Caching.CacheItemRemovedReason reason)
{
    switch (reason)
    {
        case System.Web.Caching.CacheItemRemovedReason.Removed:
        case System.Web.Caching.CacheItemRemovedReason.Underused:
            return;
        default:
            break;
    }

    // Put the object that was just removed back into the cache
    ReloadCache(key, value);

    // Recreate the cache with the new object
    PopulateCache(15);
}

Open in new window

0
 

Author Closing Comment

by:devo00
ID: 35403303
Great answers, I used jQuery but will apply caching as well. Marked partially only because one answer was in VB.
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35403456
Glad to help you dude
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question