DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> XML、DataSet、DataGrid結合寫成廣告管理程序二
XML、DataSet、DataGrid結合寫成廣告管理程序二
編輯: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;        //定義公用的DataSet
DataView dv;  //定義公用的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();
    }
}

//捆綁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[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" />
   。糰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>廣告圖片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> 

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved