net8:XML的读写操作【广告控件的XML文件实例】

2021-06-29 09:05

阅读:823

标签:can   version   来源   sub   tco   int   发布   button   style   

原文发布时间为:2008-08-05 —— 来源于本人的百度文章 [由搬家工具导入]

【用了datalist控件,datalist控件自己学会,主要知道其他按钮COMMANDNAME属性应该改为edit,update,delete】

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.Xml.XPath;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fill();
        }

    }

    protected void fill()
    {
        DataSet ds=new DataSet();
        ds.ReadXml(Server.MapPath("~/App_Data/ad.xml"));
        DataList1.DataSource=ds.Tables[0].DefaultView;
        DataList1.DataBind();

        Session["dst"]=ds;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        fill();
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        DataSet ds = (DataSet)Session["dst"];
        ds.Tables[0].Rows[e.Item.ItemIndex]["ImageUrl"] = ((TextBox)e.Item.FindControl("TextBox2")).Text.ToString();
        ds.Tables[0].Rows[e.Item.ItemIndex]["NavigateUrl"] = ((TextBox)e.Item.FindControl("TextBox5")).Text.ToString();
        ds.Tables[0].Rows[e.Item.ItemIndex]["AlternateText"] = ((TextBox)e.Item.FindControl("TextBox1")).Text.ToString();
        ds.Tables[0].Rows[e.Item.ItemIndex]["Keyword"] = ((TextBox)e.Item.FindControl("TextBox3")).Text.ToString();
        ds.Tables[0].Rows[e.Item.ItemIndex]["Impressions"] = ((TextBox)e.Item.FindControl("TextBox4")).Text.ToString();

        Session["dst"] = ds;
       ds.WriteXml(Server.MapPath("~/App_Data/ad.xml"));

        DataList1.EditItemIndex=-1;
        fill();
    }
    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;
        fill();
    }
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        DataSet ds = (DataSet)Session["dst"];
        ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[e.Item.ItemIndex]);
        Session["dst"] = ds;
        ds.WriteXml(Server.MapPath("~/App_Data/ad.xml"));

        fill();
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)Session["dst"];
        DataRow dr = ds.Tables[0].NewRow();

        dr["ImageUrl"] = url.Text.ToString();
        dr["NavigateUrl"] = ng.Text.ToString();
        dr["AlternateText"] = na.Text.ToString();
        dr["Keyword"] = kw.Text.ToString();
        dr["Impressions"] = qz.Text.ToString();

        ds.Tables[0].Rows.Add(dr);
        Session["dst"] = ds;

        ds.WriteXml(Server.MapPath("~/App_Data/ad.xml"));
        fill();
        DIV1.Visible = false;
        Panel1.Visible = false;
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        na.Focus();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        DIV1.Visible = true;
        ng.Focus();
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        string name = fu.FileName.ToString();
        //int size = Convert.ToInt32(fu.PostedFile.ContentLength);
        string type1 = fu.PostedFile.ContentType.ToString();
        string type = type1.Substring(0, 5);

        if (type == "image")
        {
            fu.PostedFile.SaveAs(Server.MapPath("~/img/") + name);
            img.ImageUrl = Server.MapPath("~/img/") + name;
            url.Text = Server.MapPath("~/img/") + name;
        }
        else
        {
            url.Text = "不是允许的类型";
        }
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default2.aspx");
    }
}

----------------------------------------------

ad.xml的内容:




    ~/img/logo.gif
    http://www.hc115.com
    洪川医药
    医院评价
    50


    http://www.baidu.com/img/baidu_logo_rw_0808_hzl.gif
    http://www.baidu.com
    百度
    百度
    60


    F:\wuwenjie\net8\img\iask.gif
    http://www.iask.com
    爱问
    爱问搜
    30

net8:XML的读写操作【广告控件的XML文件实例】

标签:can   version   来源   sub   tco   int   发布   button   style   

原文地址:http://www.cnblogs.com/handboy/p/7141595.html


评论


亲,登录后才可以留言!