Scrolling News Web Control using ASP.Net and C#


On many of the web portals/sites it is observed that latest news start scrolling from right to left.

To do this need to write JavaScript in detail, there could be number of storing news format. I have written this web control to avoid extensive use of JavaScript by using Marquee at server side.

I have tested this application on following browsers:

1. Internet Explorer 6.0
2. Netscape Navigator 7.2
3. Mozilla Firefox 1.5

So no need to worry much about cross browser performance issue, it work simply great on all above browsers.

Requirement:

Need To make following tables in SQL Server Database, and put corresponding records in the tables.

News Table

create table News
(
newsId  int  primary key identity(1,1) Not null,
newTitle varchar(50) Not null,
dateCreated datetime
)

The code behind of page i.e. on page load I have following code

News.ascx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Fill News Section
            FillNews();
        }
    }

    private void FillNews()
    {
        // Fetch News Records from Database and Store in Datatable…

        string news = null;

        for (int i = 0; i < DataTable.Rows.Count; i++)
        {
            news = news + DataTable.Rows[i]["News"].ToString();
            news = news + "  ||  ";
        }

        lblNews.Text = news;
    }

Which just prepare a connection with the database and get the newsTitle and dateCreated to scroll in the marquee in direction=’UP’. I have prepared a string which can be added dynamically in a table row to fit in appropriate table row, which generates scrolling news section with anchor (link) to open detail in the new window. Idea behind opening in new window to stick the user to same site. You can do following with the marquee and JavaScript functionality used in the same.

  • Can stop on mouseover of the link [OnMouseOver=’this.stop();’]
  • Can start on mouseout of the link [OnMouseOut=’this.start();’]
  • Can controll the speed of the scrolling [direction=’up’ scrollamount=’2′]

Register this control as below in a file in which you want to use this

<%@  Control Language="C#" AutoEventWireup="true" CodeFile="News.ascx.cs" Inherits="UserControls_News" %>
<table width="100%" cellspacing="0px" cellpadding="0px" class="NewsBgColor">
    <tr>
        <td class="Table_TD_Center">
            <marquee behavior="SCROLL" width="100%" scrolldelay="100″" onmouseover='this.stop();'
                onmouseout='this.start();'>
                <asp:Label ID="lblNews" runat="server" Text="" CssClass="RedLabel">
                </asp:Label>
            </marquee>
        </td>
    </tr>
</table>

That’s all … Happy Programming

Advertisements

16 thoughts on “Scrolling News Web Control using ASP.Net and C#

      1. // Fetch News Records from Database and Store in Datatable…

        Above comment shows that you need to fetch datatable (recordset) from database using your method of database fetching.

        Hope this will clear your doubt now.

  1. hi sir,

    I am doing the job on Asp.net technology.I developed my company website.I want to scroll news from bottom to top which control is giving this type of facility & also how can i make effects to my website with Flash files.plese send some tips or any examples to me.

    I have full of hopes to send.

    Thanking you sir.

    1. just replace your label control with hyperlink & bind your navigateurl property & your control is ready to perform as per your rerquirement !!!

  2. Hi, though it seems very simple, I had been struggling making it work on VWD 2010 Express. I wonder if you can help me out as have just started to learn VWD and am learning through practical experience.

    It would be great if you can upload the project or email, or perhaps give a more detailed help for ultra nubies like myself.

    One of the thoughts I am getting is that it needs sql connection to be established. But at some point, I got the message that system is not able to fetch records from memory.

    With me being very new, I do not understand what that means.

    Please help.

  3. I am unable to run this code, Could you please help me to fetch news record from database and store in datatable

    my database name is SalesEmpTable and it contain 3 column(EmpId, EmpName, EmpOrder)

    Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property ‘System.Data.DataTable.Rows.get’

    Source Error:

    Line 26: // Fetch News Records from Database and Store in Datatable…
    Line 27: string news = null;
    Line 28: for (int i = 0; i < DataTable.Rows.Count; i++)
    Line 29: {
    Line 30: news = news + DataTable.Rows[i]["News"].ToString();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s