Scrolling News Web Control using ASP.Net and C#
Posted by Ramani Sandeep on January 6, 2009
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
Vinod C said
Good one,
Thanks
SM said
What is DataTable in the FillNews method?
Ramani Sandeep said
// 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.
Gattukumar said
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.
ramanisandeep said
To scroll news from bottom to top : u can use tag .. & inside tag use Reapetor or Datalist Control .
Nayan said
But what if we need to display a hyperlink instead of a label?
ramanisandeep said
just replace your label control with hyperlink & bind your navigateurl property & your control is ready to perform as per your rerquirement !!!
Sunita Badsra said
I was very confused as i was new in this field.But it was so simple when I read this .Thanks a lot.
sk said
Hey, Nice post. I have researched some Excellent C#.Net interview questions and answers
Komal said
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.
vishnu said
But in IE 8 marquee will not work the scrolling is not comming in IE8.
spotify apple said
Wow, this paragraph is pleasant, my younger sister is analyzing
such things, thus I am going to tell her.
Nikunj said
Its good coding