Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » ASP.NET » Scrolling News Web Control using ASP.Net and C#

Scrolling News Web Control using ASP.Net and C#

This article shows to create scrolling news web control using ASP.Net and C#, simple to understand and can be used as a control in any of the ASP.Net page.

Author Rank :
Page Views : 11479
Downloads : 433
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ScrollingNews.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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

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.

This is a very simple table structure in Master Detail format. In tbl_News all the titles and date created will be stored and corresponding newsDetail are stored in tbl_NewsDetail.

News Table

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

News Detail Table

create table tbl_NewsDetail
(
 dtlId  int  primary key identity(1,1) Not Null,
 newsId  int foreign key references tbl_news,
 newsDetail varchar(8000)
)

Explanation in detail about the coding

In folder called as "userControll" I have web control as "Scrolling_News.ascx".

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

private void Page_Load(object sender, System.EventArgs e)

{

          // Put user code to initialize the page here

          SqlConnection myCon = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);

          string strSql = "SELECT * FROM tbl_News order by dateCreated asc";

          string strScrolling = "";

          HtmlTableCell cellScrolling = new HtmlTableCell();

          SqlCommand myComd = new SqlCommand(strSql,myCon);

          SqlDataReader sqlRdr;

          try

          {

                   myCon.Open();

                   sqlRdr = myComd.ExecuteReader();

                   strScrolling = "<Marquee OnMouseOver='this.stop();' OnMouseOut='this.start();' direction='up'
                                     scrollamount='2' bgcolor='#000000' width='40%'>";

                   while(sqlRdr.Read())

                   {

                             strScrolling = strScrolling + "<a href='#' OnClick="+"javascript:window.open
                                                ('newsDetail.aspx?NewsId="+sqlRdr.GetValue(0)
                                                +"','NewsDetail','width=400,height=400;toolbar=no;');"+"><font
                                                face='verdana' size='2' color='#ffffff'>"+ sqlRdr.GetValue(1)
                                                +"</a>&nbsp;&nbsp;"+sqlRdr.GetValue(2).ToString()+"</font><br><br>";

                   }

                   strScrolling = strScrolling +"</Marquee>";

                   sqlRdr.Close();

                   cellScrolling.InnerHtml = strScrolling;

                   rowScrolling.Cells.Add(cellScrolling);  

          }

          catch(Exception msg)

          {

                   Response.Write(msg.Message);

          }

          finally

          {

                   //close sql connection   

                   myCon.Close();

          }

}

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.

  • Change the direction of the scrolling [direction='up']
  • 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']

Microsoft also provide Add rotator component which can be used with XML, but it differs with respect that
by having above functionality.

In newsDetail.aspx page, I have again simple code to get the detail based on querystring of newsId.

Using this control in a page

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

<%@ Register TagPrefix="Scrolling" TagName="News" src="userControll/Scrolling_News.ascx" %>

and use this in a table as below

<table width="100%" bgColor="#ccccff">
<tr>
    <td width="60%"><STRONG><FONT face="Tahoma">News Section</FONT></STRONG></td>
</tr>
<tr>
    <td><SCROLLING:NEWS id="scroller" runat="server"></SCROLLING:NEWS></td>
</tr>
</
table>

That' all enjoy...

What you can do as enhancement?

As an enhancement to this tutorial/article one can add field imageURL in newsDetail table. Also an admin control panel can be provided, to add news and news details to the database by simply having additional screen, which will become complete web application

I think this code snippet is very simple and handy to use as a control in day to day life.

Next article >> I am thinking of writing dynamic drop down, Menu as web control (0 to 1 levels)

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Munir Shaikh
Munir is MCP in Microsoft .NET Framework 3.5, Windows Communication Foundation
Appl ication Developmen, software Developer/ project lead with 9 Yrs development experience who works on IT projects mainly for Microsoft and some open source technologies. Most of these projects have been intranet based web applications / sites with SQL/Oracle as back-end. Currently he  is focusing more on Silverlight, WCF & WPF development. He had worked on payment gateway implementation. He is experienced in Insurance, Supply Chain management, Trading, Real-Estate & currently Legacy modernization domain.
Apart from this he has implemented on CMMi L3 for organization and has good understanding of process. He has also involved in consulting activities like Architecture Review, Project Plan, HLD, LLD, Risk Management etc....
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
table code by Krishna On March 22, 2007
I have a table with two rows , first row display the buttons HOME , MASTER , ASSOCIATE , CONTACT Second row is my target row. When I click on HOME BUTTON its open HOME.ASPX and display in TARGET row() I means second row. (NOT USING NEW WINDOW) Same as MASTER.ASPX, ASSOCIATE.ASPX , CONTACT.ASPX How to do this in ASP.NET 2005 using C#? Plz Help krish_chand2008@yahoo.co.in
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.