Scrolling News Ticker Control in C# and ASP.Net
Steps
1) Declare a DIV tag with "runat=server" and ID tag.
2) Get the feed from www.asp.net, build a marquee string with the feed got and assign the string to InnerText property of DIV tag. See the code for better understanding.
ASCX code:
<table width="250px" border="0" cellpadding="0" id="tblTechNewsScrolling" runat="server" cellspacing="0">
<tr>
<td width="7" height="35" align="right" class="topleft"></td>
<td align="center" class="topmid"><asp:Label ID="lblNewsHeading" runat="server"></asp:Label></td>
<td width="11" height="35" align="left" class="topright"></td>
</tr>
<tr>
<td class="midleft"></td>
<td>
<div id="divNews" style="width:230px;height:190px" runat="server">
</div></td>
<td class="midright"></td>
</tr>
<tr>
<td width="7" height="11" align="right" valign="top" class="bottomleft"></td>
<td class="botmidbg"> </td>
<td align="left" valign="top" class="bottomright"></td>
</tr>
</table>
Building Marquee in CodeBehind:
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(ConfigurationManager.AppSettings["ASPNETRSS"]);
DataSet ds = new DataSet();
ds.ReadXml(reader);
DataTable dtNews = ds.Tables["item"];
StringBuilder strScrollingNews = new StringBuilder();
lblNewsHeading.Text = "ASP.Net Articles";
if (dtNews.Rows.Count > 0)
{
strScrollingNews.Append("<Marquee OnMouseOver='this.stop();' OnMouseOut='this.start();' direction='up' scrollamount='3'>");
for (int i = 0; i < dtNews.Rows.Count; i++)
{
strScrollingNews.Append("<div class='PostTitle'><a href=" + dtNews.Rows[i]["Link"].ToString() + " target='_blank'/>" + dtNews.Rows[i]["Title"].ToString() + "</a></div><div class='PostInfo'>" + dtNews.Rows[i]["Description"].ToString() + ".</div><div class='PostTitle'><a href=" + dtNews.Rows[i]["Link"].ToString() + ">Read more..</a></div><br>");
}
strScrollingNews.Append("</Marquee>");
divNews.InnerHtml = strScrollingNews.ToString();
}
else
{
divNews.InnerHtml = "No News";
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
finally
{
reader.Close();
}
In Web.Config, we can give the RSS URL.
<appSettings>
<add key ="ASPNETRSS" value="http://asp.net/community/articles/rss.ashx"/>
</appSettings>
We can use this usercontrol for getting feeds from any site by giving the RSS URL in Web.Config.
|