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)?

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!
devo00Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aternexCommented:
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
devo00Author Commented:
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
aternexCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

aternexCommented:
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
Carlos VillegasFull Stack .NET DeveloperCommented:
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
devo00Author Commented:
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
Carlos VillegasFull Stack .NET DeveloperCommented:
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
devo00Author Commented:
Great answers, I used jQuery but will apply caching as well. Marked partially only because one answer was in VB.
0
Carlos VillegasFull Stack .NET DeveloperCommented:
Glad to help you dude
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.