这个是用XML作为数据库。
用来做一些类似栏目推广啊,反正就是小型的窗口推广之类的。
上代码
- <?xml version="1.0" encoding="utf-8"?>
- <root>
- <group id="1">
- <ad id="1" lnkurl="p_w_picpaths/pto/0.jpg" imgurl="p_w_picpaths/pto/0.jpg">我爱好C#</ad>
- <ad id="2" lnkurl="p_w_picpaths/pto/1.jpg" imgurl="p_w_picpaths/pto/1.jpg">My book2</ad>
- <ad id="3" lnkurl="p_w_picpaths/pto/2.jpg" imgurl="p_w_picpaths/pto/2.jpg">My book3</ad>
- <ad id="4" lnkurl="p_w_picpaths/pto/3.jpg" imgurl="p_w_picpaths/pto/3.jpg">My book4</ad>
- </group>
- <group id="2">
- <ad id="1" lnkurl="p_w_picpaths/pto/0.jpg" imgurl="p_w_picpaths/pto/0.jpg">My book1</ad>
- <ad id="2" lnkurl="p_w_picpaths/pto/1.jpg" imgurl="p_w_picpaths/pto/1.jpg">My book2</ad>
- </group>
- </root>
以上就是XML文件
下面上ASPX文件内容
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="p_w_picpathchange.aspx.cs" Inherits="p_w_picpathchange" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <style>
- .addbk
- {
- background-color:Gray;
- float:left;
- width:900px;
- }
- .addbk ul li
- {
- float:left;
- width:250px;
- }
- .alist{width:200px;}
- #a {text-decoration:none;}
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div class="addbk">
- <ul>
- <asp:Repeater ID="rptImageShow" runat="server">
- <ItemTemplate>
- <li>
- <div class="alist">
- <u><a href="<%#Eval("lnkurl") %>"><img src="<%#Eval("imgurl") %>" /></a></u>
- <i><a href=""<%#Eval("lnkurl") %>"><%#Eval("Description")%></a></i>
- </div>
- </li>
- </ItemTemplate>
- </asp:Repeater>
- <!--
- <li>
- <div class="alist">
- <u><a href=""><img src="p_w_picpaths/pto/0.jpg" /></a></u>
- <i><a href="">hhh</a></i>
- </div>
- </li>
- <li>
- <div class="alist">
- <u><a href=""><img src="p_w_picpaths/pto/0.jpg" /></a></u>
- <i><a href="">hhh</a></i>
- </div>
- </li>
- -->
- </ul>
- </div>
- </form>
- </body>
- </html>
以下是ASPX.CS文件
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Xml;
- public partial class p_w_picpathchange : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- ImageShowHelper hp = new ImageShowHelper();
- hp.dataRoot = Server.MapPath("~/app_data/adlqy.xml");
- this.rptImageShow.DataSource = hp.GetImageListByGroupId(1);
- this.rptImageShow.DataBind();
- //IList<ImageShow> l = hp.GetImageListByGroupId(1);
- //l[0].Description = "我爱好C#";
- //hp.SaveXmlDocumentByGroupId(l, 1);
- }
- class ImageShowHelper
- {
- IList<ImageShow> ilist;
- public string dataRoot = "";
- public IList<ImageShow> GetImageListByGroupId(int groupid)
- {
- XmlDocument xmldoc = new XmlDocument();
- xmldoc.Load(dataRoot);
- ImageShow model;
- XmlNode xnlist = xmldoc.SelectSingleNode("root");
- ilist = new List<ImageShow>();
- foreach (XmlNode xn in xnlist.ChildNodes)//Get group List
- {
- if (xn.Attributes["id"].Value==groupid.ToString())//Get Group
- {
- XmlNodeList adxnlist = xn.SelectNodes("ad");//Get ad List
- foreach (XmlNode xnImageObj in adxnlist)//Get ad
- {
- model = new ImageShow();
- model.Id = Convert.ToInt32(xnImageObj.Attributes["id"].Value);
- model.Imgurl = xnImageObj.Attributes["imgurl"].Value;
- model.Lnkurl = xnImageObj.Attributes["lnkurl"].Value;
- model.Description = xnImageObj.InnerText;
- ilist.Add(model);
- }
- break;
- }
- }
- return ilist;
- }
- public bool SaveXmlDocumentByGroupId(IList<ImageShow> showlist,int groupid)
- {
- bool isSave = false;
- XmlDocument xmldoc = new XmlDocument();
- xmldoc.Load(dataRoot);
- XmlNode xnlist = xmldoc.SelectSingleNode("root");
- foreach (XmlNode xn in xnlist.ChildNodes)//Get group List
- {
- if (xn.Attributes["id"].Value == groupid.ToString())//Get Group
- {
- XmlNodeList adxnlist = xn.SelectNodes("ad");//Get ad List
- for (int i = 0; i < adxnlist.Count; i++)
- {
- XmlNode xnImageObject = adxnlist.Item(i);
- xnImageObject.Attributes["imgurl"].Value=showlist[i].Imgurl;
- xnImageObject.Attributes["lnkurl"].Value=showlist[i].Lnkurl;
- xnImageObject.InnerText = showlist[i].Description;
- }
- break;
- }
- }
- xmldoc.Save(dataRoot);
- return isSave;
- }
- }
- class ImageShow
- {
- int id;
- public int Id
- {
- get { return id; }
- set { id = value; }
- }
- string lnkurl;
- public string Lnkurl
- {
- get { return lnkurl; }
- set { lnkurl = value; }
- }
- string imgurl;
- public string Imgurl
- {
- get { return imgurl; }
- set { imgurl = value; }
- }
- string description;
- public string Description
- {
- get { return description; }
- set { description = value; }
- }
- }
- }
说一下思路:
因为某些客户,老是要在网页的某个地方插入一些广告。
所以就把广告这个抽象了一下~
我主要是想分组,用来为以后扩展做准备。
但是平常用也就一个组~所以就写了两个方法,根据组ID来存储数据~
多组以后补上吧~
就是这样了,源码就不上传了。
转载于:https://blog.51cto.com/snowleung/435870