要实现滚动功能通常是用脚本代码编写而成的,很繁琐!这里给大家介绍用web控件实现其功能!实现这个功能的前提是要设计一个主从详细显示的数据库,为方便操作,我在系统给的NorthWind数据库中加入了两个表,这两个表结构很简单的!
表一
News Table:
createtable
(
newsId int primary key identity(1,1) Not null,
newTitle varchar(50) Not null,
dateCreated datetime
)
表二
News Detail Table
createtable
(
dtlId int primary key identity(1,1) Not Null,
newsId int foreign key references tbl_news,
newsDetail varchar(8000)
)
完后,可以随意的输入些新闻的信息,要不然程序运行的时候,将看不到任何效果!
关于代码的详细说明:
1、建立的控件"Scrolling_News.ascx"放在文件夹“userControll”中,这只是为了便于有效管理!在该控件中,我放置了一个table控件,相应的HTML源码如下:
<TABLEid="tblScrolling" cellSpacing="1" cellPadding="1" width="100%" border="0" runat="server">
<TRid="rowScrolling" runat="server"></TR>
</TABLE>
这个控件主要是查找新闻主表,按照创建日期进行排序,给出当前新闻的宏观展示。相关代码如下:
private void Page_Load(object sender, System.EventArgs
{
new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
string strSql = "SELECT * FROM tbl_News order by dateCreated asc";
string strScrolling = "";
HtmlTableCell cellScrolling = new HtmlTableCell();
new
SqlDataReader sqlRdr;
try
{
myCon.Open();
sqlRdr = myComd.ExecuteReader();
"<Marquee OnMouseOver='this.stop();' OnMouseOut='this.start();' direction='up' scrollamount='2' bgcolor='#000000' width='40%'>";
while
{
"<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> " + sqlRdr.GetValue(2).ToString() + "</font><br><br>";
}
"</Marquee>";
sqlRdr.Close();
cellScrolling.InnerHtml = strScrolling;
rowScrolling.Cells.Add(cellScrolling);
}
catch (Exception
{
Response.Write(msg.Message);
}
finally
{
myCon.Close();
}
}
、在newsdetails.aspx中,其根据传递过来的新闻标号进行处理,读取对应的新闻详细信息,并将其内容添加到newsdetails.aspx页面中显示出来,该页面的大小等信息已经限定,表面上看来好象是以个弹出窗口的形式显示!详细的代码如下:
privatevoid Page_Load(object sender, System.EventArgs
{
new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
string strSql = "SELECT newsDetail FROM tbl_NewsDetail WHERE newsId=" + Convert.ToInt32(Request.QueryString["NewsId"]);
HtmlTableCell cellNewsDetail = new HtmlTableCell();
new
SqlDataReader sqlRdr;
try
{
myCon.Open();
sqlRdr = myComd.ExecuteReader();
if
{
cellNewsDetail.InnerText = sqlRdr.GetValue(0).ToString();
rowNewsDetail.Cells.Add(cellNewsDetail);
}
else
{
"No Record found in the detail table");
}
sqlRdr.Close();
}
catch (Exception
{
Response.Write(msg.Message);
}
finally
{
myCon.Close();
}
}
到这一步就实现了滚动功能