XML、DataSet、DataGrid结合写成广告管理程序(二)

  • 来源: 互联网 作者: rocket   2008-03-24/15:04
  • 源代码如下:

    <% @ Page Language="C#" ResponseEncoding="gb2312" %>
    <% @ Import Namespace="System.Data" %>
    <% @ Import Namespace="System.Xml" %>
    <% @ Import Namespace="System.IO" %>
    <Script Language="C#" Runat="Server">
    DataSet ds;//定义公用的DataSet
    DataView dv;//定义公用的DataView
    string SortField;
    string sPath;
    public void Page_Load(Object src,EventArgs e)
    {
    if(State==null)
    {
    sPath = Server.MapPath(".") + "\\AdBanners\\ad_gb.xml";
    ds = new DataSet();
    ds.ReadXml(sPath);
    State = ds;
    }
    else
    {
    ds = (DataSet)State;
    }
    dv = ds.Tables.DefaultView;
    dv.Sort = "ImageUrl";

    if(!Page.IsPostBack)
    {
    CreateTable();
    }
    }

    //捆绑Binder
    public void CreateTable()
    {

    dgXML.DataSource = dv;
    dgXML.DataBind();
    }

    //翻页时
    public void dgXML_Changed(Object sender,DataGridPageChangedEventArgs e)
    {
    CreateTable();
    }

    //删除
    public void DelItem(Object sender,DataGridCommandEventArgs e)
    {
    if(((LinkButton)e.CommandSource).CommandName == "del")
    {
    //首先取得当前更新页的行数与CurrentPageIndex
    int CPI = (int)dgXML.CurrentPageIndex;
    int EII = (int)e.Item.ItemIndex;
    int row = CPI*5+EII;

    lb.Text = row.ToString();

    //删除
    dv.Delete(row);

    dgXML.EditItemIndex = -1;
    ds.WriteXml(sPath);

    CreateTable();
    }
    }

    //取消
    public void dgXML_Cancel(Object sender,DataGridCommandEventArgs e)
    {
    dgXML.EditItemIndex = -1;
    CreateTable();
    }

    //编辑
    public void dgXML_Edit(Object sender,DataGridCommandEventArgs e)
    {
    dgXML.EditItemIndex = (int)e.Item.ItemIndex;
    CreateTable();
    }

    //更新
    public void dgXML_Update(Object sender, DataGridCommandEventArgs e)
    {
    try
    {
    //首先取得当前更新页的行数与CurrentPageIndex
    int CPI = (int)dgXML.CurrentPageIndex;
    int EII = (int)e.Item.ItemIndex;
    int row = CPI*5+EII;
    //lb.Text = row.ToString();

    //取得各项值

    string ImageUrl = ((TextBox)e.Item.Cells).Text;
    string NavigateUrl= ((TextBox)e.Item.Cells).Text;
    string AlternateText= ((TextBox)e.Item.Cells).Text;
    string Keyword= ((TextBox)e.Item.Cells).Text;
    string Impressions= ((TextBox)e.Item.Cells).Text;

    dv.Delete(row);

    DataRow dr = ds.Tables.NewRow();
    dr=ImageUrl;
    dr=NavigateUrl;
    dr=AlternateText;
    dr=Keyword;
    dr=Impressions;
    ds.Tables.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.NewRow();
    dr=mUrl.Text;
    dr=aUrl.Text;
    dr=mText.Text;
    dr=aKey.Text;
    dr=aTime.Text;
    ds.Tables.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" />
    。糰sp: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>广告图片



    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多