当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  用ASP.NET结合XML制作广告管理程序(1)

 用ASP.NET结合XML制作广告管理程序(1)

点击次数:34 次 发布日期:2008-11-26 14:13:17 作者:源代码网
源代码网推荐      本文介绍一下ASP.Net中对XML的用法,这里飞刀我只是展现了XML与DataSet相结合的用法。其实ASP.Net对XML的操作的方法与对象是很多的,比如XmlDataDocument,XmlTextReader等,这些对象功能很全,也很实用。
源代码网推荐  
源代码网推荐    本文以Adrotator控件使用的XML为例(新手可以先看一下Adrotator控件的用法),写出了一个广告管理程序。这个程序,飞刀用到的控件主要是DataGrid,可以说已经用到了DataGrid几乎全部功能,大家在关注XML的同时,可以同时学到DataGrid使用方法。
源代码网推荐  
源代码网推荐    先将本例中使用的一些方法对大家说明一下:
源代码网推荐  
源代码网推荐    DataSet主要是使用的ReadXml与WriteXml方法。它们的功能看名字就知道了,飞刀也不罗嗦了。
源代码网推荐  
源代码网推荐    DataGrid中使用的了很多的方法和对象。比如分页使用AllowPaging属性,排序使用AllowSorting属性,本例中使用了所有DataGrid中的事件,比如OnItemCommand,OnEditCommand,OnCancelCommand,OnUpdateCommand等。
源代码网推荐  
源代码网推荐    大家认真查看这些事件的处理。
源代码网推荐  
源代码网推荐    另外大家在看程序时,再想想,如果这个程序使用ASP来写需要多少页,呵呵,用ASP.Net只需要一页,而且无限次使用,这就是ASP.Net的优点。
源代码网推荐  
源代码网推荐    程序中飞刀我做了些简单的注释,最重要的还是大家的理解。呵呵,最近我们工作室的人是忙得一踏糊涂,所以就请大家原谅啦。飞刀这些日子不是不写程序,只是写的程序都需要发表在书上,(比如和追捕软件一样功能的Web网页啦,真的可以实现与追捕一样的功能,绝无虚假)不能贴在网上啦。这样会被骂的:)
源代码网推荐  
源代码网推荐    先看看我们操作的XML文件的内容:
源代码网推荐  
源代码网推荐   
源代码网推荐  <?xml version="1.0" standalone="yes"?>
源代码网推荐  <Advertisements>
源代码网推荐  <xsd:schema id="Advertisements" targetNamespace="" xmlns=""
源代码网推荐  xmlns:xsd="http://www.w3.org/1999/XMLSchema"
源代码网推荐  xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
源代码网推荐  <xsd:element name="Ad">
源代码网推荐  <xsd:complexType content="elementOnly">
源代码网推荐  <xsd:all>
源代码网推荐  <xsd:element name="ImageUrl" minOccurs="0" type="xsd:string"/>
源代码网推荐  <xsd:element name="NavigateUrl" minOccurs="0" type="xsd:string"/>
源代码网推荐  <xsd:element name="AlternateText" minOccurs="0" type="xsd:string"/>
源代码网推荐  <xsd:element name="Keyword" minOccurs="0" type="xsd:string"/>
源代码网推荐  <xsd:element name="Impressions" minOccurs="0" type="xsd:string"/>
源代码网推荐  </xsd:all>
源代码网推荐  </xsd:complexType>
源代码网推荐  </xsd:element>
源代码网推荐  <xsd:element name="Advertisements" msdata:IsDataSet="True">
源代码网推荐  <xsd:complexType>
源代码网推荐  <xsd:choice maxOccurs="unbounded">
源代码网推荐  <xsd:element ref="Ad"/>
源代码网推荐  </xsd:choice>
源代码网推荐  </xsd:complexType>
源代码网推荐  </xsd:element>
源代码网推荐  </xsd:schema>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>aspcn_bbs1.gifc</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com/bbs</NavigateUrl>
源代码网推荐  <AlternateText>ASPCN技术论坛/AlternateText>
源代码网推荐  <Keyword>bbs</Keyword>
源代码网推荐  <Impressions>50</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>aspcn_bbs3.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com/bbs</NavigateUrl>
源代码网推荐  <AlternateText>ASPCN技术论坛/AlternateText>
源代码网推荐  <Keyword>bbs</Keyword>
源代码网推荐  <Impressions>50</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>aspcn_bbs2.gifc</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com/bbs</NavigateUrl>
源代码网推荐  <AlternateText>ASPCN技术论坛/AlternateText>
源代码网推荐  <Keyword>bbs2</Keyword>
源代码网推荐  <Impressions>50</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com</NavigateUrl>
源代码网推荐  <AlternateText>我是中国人/AlternateText>
源代码网推荐  <Keyword>中国人/Keyword>
源代码网推荐  <Impressions>80</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com</NavigateUrl>
源代码网推荐  <AlternateText>我是中国人/AlternateText>
源代码网推荐  <Keyword>中国人/Keyword>
源代码网推荐  <Impressions>80</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com</NavigateUrl>
源代码网推荐  <AlternateText>我是中国人/AlternateText>
源代码网推荐  <Keyword>中国人/Keyword>
源代码网推荐  <Impressions>80</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com</NavigateUrl>
源代码网推荐  <AlternateText>我是中国人/AlternateText>
源代码网推荐  <Keyword>中国人/Keyword>
源代码网推荐  <Impressions>80</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com</NavigateUrl>
源代码网推荐  <AlternateText>我是中国人/AlternateText>
源代码网推荐  <Keyword>中国人/Keyword>
源代码网推荐  <Impressions>80</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com</NavigateUrl>
源代码网推荐  <AlternateText>我是中国人/AlternateText>
源代码网推荐  <Keyword>中国人/Keyword>
源代码网推荐  <Impressions>80</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com</NavigateUrl>
源代码网推荐  <AlternateText>我是中国人/AlternateText>
源代码网推荐  <Keyword>中国人/Keyword>
源代码网推荐  <Impressions>80</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com</NavigateUrl>
源代码网推荐  <AlternateText>我是中国人/AlternateText>
源代码网推荐  <Keyword>中国人/Keyword>
源代码网推荐  <Impressions>80</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com</NavigateUrl>
源代码网推荐  <AlternateText>我是中国人/AlternateText>
源代码网推荐  <Keyword>中国人/Keyword>
源代码网推荐  <Impressions>80</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl>http://www.aspcn.com</NavigateUrl>
源代码网推荐  <AlternateText>aaaddd</AlternateText>
源代码网推荐  <Keyword>aaadd</Keyword>
源代码网推荐  <Impressions>98090</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://www.aspcn.com/aspcn.gif</ImageUrl>
源代码网推荐  <NavigateUrl></NavigateUrl>
源代码网推荐  <AlternateText>aaasdfasfad</AlternateText>
源代码网推荐  <Keyword>aaadd</Keyword>
源代码网推荐  <Impressions>98090</Impressions>
源代码网推荐  </Ad>
源代码网推荐  <Ad>
源代码网推荐  <ImageUrl>http://wwwaspcn.com</ImageUrl>
源代码网推荐  <NavigateUrl>http://</NavigateUrl>
源代码网推荐  <AlternateText></AlternateText>
源代码网推荐  <Keyword></Keyword>
源代码网推荐  <Impressions></Impressions>
源代码网推荐  </Ad>
源代码网推荐  </Advertisements>
源代码网推荐  
源代码网推荐  
源代码网推荐    中文有点问题,但是在下面的程序中可以正常显示的,因为用了国际化技术,哈哈,这个例子用的东东可不少哟:
源代码网推荐  
源代码网推荐    这是Adrotator控件引用的标准格式XML。
源代码网推荐  
源代码网推荐    我们再来看看ASP.Net的源代码:
源代码网推荐  
源代码网推荐    由于XML文件占了很大的篇幅,只好把源程序贴在这一页了。(也是很长的哟,大家有性子看才行)
源代码网推荐  
源代码网推荐   
源代码网推荐  <% @ Page Language="C#" ResponseEncoding="gb2312" %>
源代码网推荐  <% @ Import Namespace="System.Data" %>
源代码网推荐  <% @ Import Namespace="System.Xml" %>
源代码网推荐  <% @ Import Namespace="System.IO" %>
源代码网推荐  <Script Language="C#" Runat="Server">
源代码网推荐   DataSet ds; file://定义公用的DataSet
源代码网推荐   DataView dv; file://定义公用的DataView
源代码网推荐   string SortField;
源代码网推荐   string sPath;
源代码网推荐   public void Page_Load(Object src,EventArgs e)
源代码网推荐   {
源代码网推荐    if(State["adxml"]==null)
源代码网推荐    {
源代码网推荐     sPath = Server.MapPath(".") + "\AdBanners\ad_gb.xml";
源代码网推荐     ds = new DataSet();
源代码网推荐     ds.ReadXml(sPath);
源代码网推荐     State["adxml"] = ds;
源代码网推荐    }
源代码网推荐    else
源代码网推荐    {
源代码网推荐     ds = (DataSet)State["adxml"];
源代码网推荐    }
源代码网推荐     dv = ds.Tables[0].DefaultView;
源代码网推荐     dv.Sort = "ImageUrl";
源代码网推荐  
源代码网推荐    if(!Page.IsPostBack)
源代码网推荐    {
源代码网推荐     CreateTable();
源代码网推荐    }
源代码网推荐   }
源代码网推荐  
源代码网推荐   file://捆绑Binder
源代码网推荐   public void CreateTable()
源代码网推荐   {
源代码网推荐  
源代码网推荐    dgXML.DataSource = dv;
源代码网推荐    dgXML.DataBind();
源代码网推荐   }
源代码网推荐  
源代码网推荐   file://翻页时
源代码网推荐   public void dgXML_Changed(Object   sender,DataGridPageChangedEventArgs e)
源代码网推荐   {
源代码网推荐    CreateTable();
源代码网推荐   }
源代码网推荐  
源代码网推荐   file://删除
源代码网推荐   public void DelItem(Object sender,DataGridCommandEventArgs e)
源代码网推荐   {
源代码网推荐    if(((LinkButton)e.CommandSource).CommandName == "del")
源代码网推荐    {
源代码网推荐     file://首先取得当前更新页的行数与CurrentPageIndex
源代码网推荐     int CPI = (int)dgXML.CurrentPageIndex;
源代码网推荐     int EII = (int)e.Item.ItemIndex;
源代码网推荐     int row = CPI*5+EII;
源代码网推荐  
源代码网推荐     lb.Text = row.ToString();
源代码网推荐  
源代码网推荐     file://删除
源代码网推荐     dv.Delete(row);
源代码网推荐  
源代码网推荐     dgXML.EditItemIndex = -1;
源代码网推荐     ds.WriteXml(sPath);
源代码网推荐  
源代码网推荐     CreateTable();
源代码网推荐    }
源代码网推荐   }
源代码网推荐  
源代码网推荐   file://取消
源代码网推荐  public void dgXML_Cancel(Object sender,DataGridCommandEventArgs e)
源代码网推荐   {
源代码网推荐    dgXML.EditItemIndex = -1;
源代码网推荐    CreateTable();
源代码网推荐   }
源代码网推荐  
源代码网推荐    file://编辑
源代码网推荐  public void dgXML_Edit(Object sender,DataGridCommandEventArgs e)
源代码网推荐  {
源代码网推荐   dgXML.EditItemIndex = (int)e.Item.ItemIndex;
源代码网推荐   CreateTable();
源代码网推荐   }
源代码网推荐  
源代码网推荐   file://更新
源代码网推荐  public void dgXML_Update(Object sender, DataGridCommandEventArgs e)
源代码网推荐  {
源代码网推荐   try
源代码网推荐   {
源代码网推荐   file://首先取得当前更新页的行数与CurrentPageIndex
源代码网推荐   int CPI = (int)dgXML.CurrentPageIndex;
源代码网推荐   int EII = (int)e.Item.ItemIndex;
源代码网推荐   int row = CPI*5+EII;
源代码网推荐   file://lb.Text = row.ToString();
源代码网推荐  
源代码网推荐   file://取得各项值
源代码网推荐  
源代码网推荐   string ImageUrl = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
源代码网推荐   string NavigateUrl = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
源代码网推荐   string AlternateText = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
源代码网推荐   string Keyword = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
源代码网推荐   string Impressions = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
源代码网推荐  
源代码网推荐   dv.Delete(row);
源代码网推荐  
源代码网推荐  
源代码网推荐   DataRow dr = ds.Tables[0].NewRow();
源代码网推荐   dr[0] = ImageUrl;
源代码网推荐   dr[1] = NavigateUrl;
源代码网推荐   dr[2] = AlternateText;
源代码网推荐   dr[3] = Keyword;
源代码网推荐   dr[4] = Impressions;
源代码网推荐   ds.Tables[0].Rows.Add(dr);
源代码网推荐   ds.WriteXml(sPath);
源代码网推荐   }
源代码网推荐   catch(Exception ee)
源代码网推荐   {
源代码网推荐    lb.Text = ee.ToString();
源代码网推荐   }
源代码网推荐  
源代码网推荐   dgXML.EditItemIndex = -1;
源代码网推荐   CreateTable();
源代码网推荐  
源代码网推荐  
源代码网推荐   }
源代码网推荐  
源代码网推荐  public void PanelShow(Object sender,EventArgs e)
源代码网推荐  {
源代码网推荐   AddItem.Visible = true;
源代码网推荐  }
源代码网推荐  
源代码网推荐  public void AddItem_Click(Object sender,EventArgs e)
源代码网推荐  {
源代码网推荐   DataRow dr = ds.Tables[0].NewRow();
源代码网推荐   dr[0] = mUrl.Text;
源代码网推荐   dr[1] = aUrl.Text;
源代码网推荐   dr[2] = mText.Text;
源代码网推荐   dr[3] = aKey.Text;
源代码网推荐   dr[4] = aTime.Text;
源代码网推荐   ds.Tables[0].Rows.Add(dr);
源代码网推荐   ds.WriteXml(sPath);
源代码网推荐   CreateTable();
源代码网推荐   AddItem.Visible=false;
源代码网推荐  }
源代码网推荐  </script>
源代码网推荐  <html>
源代码网推荐  <head>
源代码网推荐  <title></title>
源代码网推荐  </head>
源代码网推荐  <body>
源代码网推荐  <form id="form1" runat="server">
源代码网推荐  <asp:DataGrid id="dgXML" runat="server"
源代码网推荐   AllowPaging="True"
源代码网推荐   PageSize="10"
源代码网推荐   BorderColor="black"
源代码网推荐   BorderWidth="1"
源代码网推荐   CellPadding="3"
源代码网推荐   Font-Name="Verdana"
源代码网推荐   Font-Size="8pt"
源代码网推荐   HeaderStyle-BackColor="#aaaadd"
源代码网推荐   PagerStyle-Mode="NumericPages"
源代码网推荐   PagerStyle-HorizontalAlign="Right"
源代码网推荐   PagerStyle-PrevPageText="前页"
源代码网推荐   PagerStyle-NextPageText="后页"
源代码网推荐   OnPageIndexChanged="dgXML_Changed"
源代码网推荐   OnEditCommand="dgXML_Edit"
源代码网推荐   OnCancelCommand="dgXML_Cancel"
源代码网推荐   OnUpdateCommand="dgXML_Update"
源代码网推荐   OnItemCommand="DelItem"
源代码网推荐   AutoGenerateColumns="false"
源代码网推荐  >
源代码网推荐  <property name="Columns">
源代码网推荐  <asp:ButtonColumn HeaderText="删除" Text="删除" CommandName="del" />
源代码网推荐  <asp:EditCommandColumn EditText="修改" CancelText="取消" UpdateText="更新" ItemStyle-Wrap="false"
源代码网推荐  HeaderText="操作区" HeaderStyle-Wrap="false" />
源代码网推荐  <asp:BoundColumn HeaderText="图片地址(相对)" SortField="ImageUrl" DataField="ImageUrl" />
源代码网推荐  <asp:BoundColumn HeaderText="链接URL" SortField="NavigateUrl" DataField="NavigateUrl" />
源代码网推荐  <asp:BoundColumn HeaderText="图片说明" SortField="AlternateText" DataField="AlternateText" />
源代码网推荐  <asp:BoundColumn HeaderText="类别" SortField="Keyword" DataField="Keyword" />
源代码网推荐  <asp:BoundColumn HeaderText="显示时间" SortField="Impressions" DataField="Impressions" />
源代码网推荐  </property>
源代码网推荐  </asp:DataGrid>
源代码网推荐  <hr>
源代码网推荐  <asp:Button id="vi" Text="添加新项" OnClick="PanelShow" runat="server"/>
源代码网推荐  <br>
源代码网推荐  <asp:Panel id="AddItem" runat="server" Visible="false">
源代码网推荐  <table>
源代码网推荐  <tr Bgcolor="#aaaadd"><td colspan=2>添加新的广告页面</td></tr>
源代码网推荐  <tr>
源代码网推荐  <td>广告图片URL:</td>
源代码网推荐  <td><asp:TextBox id="mUrl" runat="server" Text="http://" /></td>
源代码网推荐  </tr>
源代码网推荐  <tr>
源代码网推荐  <td>广告链接地址:</td>
源代码网推荐  <td><asp:TextBox id="aUrl" runat="server" Text="http://" /></td>
源代码网推荐  </tr>
源代码网推荐  <tr>
源代码网推荐  <td>图片说明:</td>
源代码网推荐  <td><asp:TextBox id="mText" runat="server" /></td>
源代码网推荐  </tr>
源代码网推荐  <tr>
源代码网推荐  <td>广告所属类别:</td>
源代码网推荐  <td><asp:TextBox id="aKey" runat="server" /></td>
源代码网推荐  </tr>
源代码网推荐  <tr>
源代码网推荐  <td>显示时间</td>
源代码网推荐  <td><asp:TextBox id="aTime" runat="server" /></td>
源代码网推荐  </tr>
源代码网推荐  <tr><td>
源代码网推荐  <asp:Button id="submit" Text="提交" OnClick="AddItem_Click" runat="server" /></td></tr>
源代码网推荐  </table>
源代码网推荐  </asp:Panel>
源代码网推荐  <asp:Label id="lb" runat="server" />
源代码网推荐  </form>
源代码网推荐  </body>
源代码网推荐  </html>
源代码网推荐  
源代码网推荐    大家好好分析吧。
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华