滚动新闻图片的动态更新
朋友要做个小网站,首页要实现如图 滚动图片新闻,找了一下相关的代码,可是怎么实现动态更新呢?
好像只能通过xml来处理了。
思路:
1.当保存动态新闻图片的数据库表由更新完成时时,就再查询一次表,取前5条,将其保存到xml文件,页面数据直接从xml文件中读取。
2.在页面的cs文件中 通过xmldocument和response.Write将内容输出到页面。
具体的步骤代码如下:
在后台动态新闻图片管理更新时,查询表生成xml文件
private void createXmldocument()

{

创建xml文件#region 创建xml文件
string commandText = " select top 6 * from Pic";
conn = new SqlConnection(DbConnection.ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(commandText, conn);
DataSet ds = new DataSet();
da.Fill(ds);

XmlDocument xmldoc = new XmlDocument();//创建xml文档
XmlDeclaration desc = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "yes");//创建声明
xmldoc.AppendChild(desc);

XmlElement root = xmldoc.CreateElement("picture");//创建根元素
xmldoc.AppendChild(root);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

{
XmlElement pic = xmldoc.CreateElement("pic");
XmlElement picUrl = xmldoc.CreateElement("picUrl");
picUrl.InnerText = ds.Tables[0].Rows[i][1].ToString();
pic.AppendChild(picUrl);
XmlElement picPath = xmldoc.CreateElement("picPath");
picPath.InnerText = ds.Tables[0].Rows[i][2].ToString();
pic.AppendChild(picPath);
XmlElement picText = xmldoc.CreateElement("picText");
picText.InnerText = ds.Tables[0].Rows[i][4].ToString();
pic.AppendChild(picText);
xmldoc.DocumentElement.AppendChild(pic);
}
string xmlpath = Server.MapPath("~/picture.xml");
xmldoc.Save(xmlpath);
#endregion
}
下面就是使用xml文件了,

Code
1
StringBuilder texts = new StringBuilder();
2
StringBuilder paths = new StringBuilder();
3
XmlDocument doc = new XmlDocument();
4
string str = Server.MapPath("~/picture.xml");
5
doc.Load(str);
6
int i;
7
XmlNode node;
8
XmlElement pic = doc.DocumentElement;//取出pic的节点
9
for (i = 0; i < pic.ChildNodes.Count - 1; i++)
10
{
11
node = pic.ChildNodes[i];
12
texts.Append(node.ChildNodes[0].InnerText);
13
texts.Append("|");
14
paths.Append(node.ChildNodes[1].InnerText);
15
paths.Append("|");
16
17
}
18
node = pic.ChildNodes[i];
19
texts.Append(node.ChildNodes[0].InnerText);
20
paths.Append(node.ChildNodes[1].InnerText);
21
22
int focus_width =195;
23
int focus_height = 160;
24
int text_height = 15;
25
int swf_height = focus_height + text_height;
26
StringBuilder script = new StringBuilder();
27
script.Append("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"" + focus_width + "\"" + " height=\"" + swf_height + "\"/>");
28
script.Append("<param name=\"allowScriptAccess\" value=\"sameDomain\"/> <param name=\"movie\" value=\"focus.swf\"/><param name=\"quality\" value=\"high\"/><param name=\"bgcolor\" value=\"#F0F0F0\"/>");
29
script.Append("<param name=\"menu\" value=\"false\"/><param name=\"wmode\" value=\"opaque\"/>");
30
script.Append("<param name=\"FlashVars\" value=\"pics=" + paths + "&texts=" + texts + "&borderwidth=" + focus_width + "&borderheight=" + focus_height + "&textheight=" + text_height + "\"/>");
31
script.Append("<embed src=\"pixviewer.swf\" wmode=\"opaque\" FlashVars=\"pics=" + paths + "&texts=" + texts + "&borderwidth=" + focus_width + "&borderheight=" + focus_height + "&textheight=" + text_height + " \"menu=\"false\" bgcolor=\"#F0F0F0\" quality=\"high\" width=\"" + focus_width + " \" height=\"" + focus_height + " \"allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"/>");
32
script.Append("</object>");
33
this.Response.Write(script);
这样基本就实现了动态更新了滚动新闻图片。尝试过用xmlRequestHttp来访问,不过将数据显示到页面时没能显示出来。
好像只能通过xml来处理了。思路:
1.当保存动态新闻图片的数据库表由更新完成时时,就再查询一次表,取前5条,将其保存到xml文件,页面数据直接从xml文件中读取。
2.在页面的cs文件中 通过xmldocument和response.Write将内容输出到页面。
具体的步骤代码如下:
在后台动态新闻图片管理更新时,查询表生成xml文件
private void createXmldocument()
{
创建xml文件#region 创建xml文件
string commandText = " select top 6 * from Pic";
conn = new SqlConnection(DbConnection.ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(commandText, conn);
DataSet ds = new DataSet();
da.Fill(ds);
XmlDocument xmldoc = new XmlDocument();//创建xml文档
XmlDeclaration desc = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "yes");//创建声明
xmldoc.AppendChild(desc);
XmlElement root = xmldoc.CreateElement("picture");//创建根元素
xmldoc.AppendChild(root);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
XmlElement pic = xmldoc.CreateElement("pic");
XmlElement picUrl = xmldoc.CreateElement("picUrl");
picUrl.InnerText = ds.Tables[0].Rows[i][1].ToString();
pic.AppendChild(picUrl);
XmlElement picPath = xmldoc.CreateElement("picPath");
picPath.InnerText = ds.Tables[0].Rows[i][2].ToString();
pic.AppendChild(picPath);
XmlElement picText = xmldoc.CreateElement("picText");
picText.InnerText = ds.Tables[0].Rows[i][4].ToString();
pic.AppendChild(picText);
xmldoc.DocumentElement.AppendChild(pic);
}
string xmlpath = Server.MapPath("~/picture.xml");
xmldoc.Save(xmlpath);
#endregion
}1
StringBuilder texts = new StringBuilder();2
StringBuilder paths = new StringBuilder();3
XmlDocument doc = new XmlDocument();4
string str = Server.MapPath("~/picture.xml");5
doc.Load(str);6
int i;7
XmlNode node;8
XmlElement pic = doc.DocumentElement;//取出pic的节点9
for (i = 0; i < pic.ChildNodes.Count - 1; i++)10

{11
node = pic.ChildNodes[i];12
texts.Append(node.ChildNodes[0].InnerText);13
texts.Append("|");14
paths.Append(node.ChildNodes[1].InnerText);15
paths.Append("|");16
17
}18
node = pic.ChildNodes[i];19
texts.Append(node.ChildNodes[0].InnerText);20
paths.Append(node.ChildNodes[1].InnerText);21

22
int focus_width =195;23
int focus_height = 160;24
int text_height = 15;25
int swf_height = focus_height + text_height;26
StringBuilder script = new StringBuilder();27
script.Append("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"" + focus_width + "\"" + " height=\"" + swf_height + "\"/>");28
script.Append("<param name=\"allowScriptAccess\" value=\"sameDomain\"/> <param name=\"movie\" value=\"focus.swf\"/><param name=\"quality\" value=\"high\"/><param name=\"bgcolor\" value=\"#F0F0F0\"/>");29
script.Append("<param name=\"menu\" value=\"false\"/><param name=\"wmode\" value=\"opaque\"/>");30
script.Append("<param name=\"FlashVars\" value=\"pics=" + paths + "&texts=" + texts + "&borderwidth=" + focus_width + "&borderheight=" + focus_height + "&textheight=" + text_height + "\"/>");31
script.Append("<embed src=\"pixviewer.swf\" wmode=\"opaque\" FlashVars=\"pics=" + paths + "&texts=" + texts + "&borderwidth=" + focus_width + "&borderheight=" + focus_height + "&textheight=" + text_height + " \"menu=\"false\" bgcolor=\"#F0F0F0\" quality=\"high\" width=\"" + focus_width + " \" height=\"" + focus_height + " \"allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"/>");32
script.Append("</object>"); 33
this.Response.Write(script);

浙公网安备 33010602011771号