Repeater、GridView等 操作XML文件

在项目中需要对XML文件进行管理,我用了Repeater 的方式来实现了实时的更新

希望对阅读这篇随笔的您有所帮助,我用的Repeater,您也可以用GridView等...区别仅仅在于控件内的按钮的事件中获取当前操作对象索引的方式..

如果您的XML文档格式与此不同,建议使用下面的XML格式(因为这个格式能够通过DataSet的LoadXML方法直接获得数据源,并且能够使用DateSet的WriteXML方法输出XML文档)或者您可以根基自己的格式重新写ReadXmlInfoWriteXMLInfo这两个方法。

首先是我的XML格式

代码
<?xml version="1.0" standalone="yes"?>
<imgs>
<pic name="/adv_pic/1.jpg" url="http://www.baidu.com/" title="美国影视中的黑人总统盘点" />
<pic name="/adv_pic/2.jpg" url="http://www.baidu.com/" title="斯琴格日乐--我很好!" />
<pic name="/adv_pic/3.jpg" url="http://www.baidu.com/" title="伦敦金融城市长秀" />
<pic name="/adv_pic/4.jpg" url="http://www.baidu.com/" title="市长大人的金色大马车" />
<pic name="/adv_pic/5.jpg" url="http://www.baidu.com/" title="英国皇家歌剧院艺术家为公益缘何全裸?" />
</imgs>

HTML Code:

代码
<%@ Register Assembly="Anthem" Namespace="Anthem" TagPrefix="anthem" %>
<!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>flash橱窗管理</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<link href="../css/css.css" type="text/css" rel="stylesheet">
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center;">
<anthem:Repeater ID="Repeater1" runat="server" AutoUpdateAfterCallBack="true">
<HeaderTemplate>
<table class="DataList" cellspacing="0" rules="all" border="1" id="Datagrid2" style="width: 100%;
border-collapse: separate;">
<tr class="DataList_Head" align="Left">
<td align="center" style="width: 2%;">
编号
</td>
<td align="center" style="width: 30%;">
标题
</td>
<td align="center" style="width: 30%;">
地址
</td>
<td align="center" style="width: 30%;">
图片
</td>
<td align="center" style="width: 8%;">
操作
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="DataList_Item" align="Left">
<td align="center" style="width: 2%;">
<%#Container.ItemIndex%></td>
<td align="center" style="width: 30%;">
<a href='<%# DataBinder.Eval(Container.DataItem,"url")%>'
target
="_blank"><%# DataBinder.Eval(Container.DataItem,"title")%></a>
</td>
<td align="center" style="width: 30%;">
<a href='<%# DataBinder.Eval(Container.DataItem,"url")%>'
target
="_blank">
<%#DataBinder.Eval(Container.DataItem, "url").ToString()%></a>
</td>
<td align="center" style="width: 30%;">
<a href='<%# DataBinder.Eval(Container.DataItem,"name")%>'
target
="_blank"><%# DataBinder.Eval(Container.DataItem,"name")%></a>
</td>
<td align="center" style="width: 10%;">
<anthem:LinkButton Text="修改" ID="btn_Edit" OnClick="btn_Edit_Click" runat="server" ToolTip='<%#Container.ItemIndex%>'></anthem:LinkButton>
<anthem:LinkButton Text="删除" ID="btn_Delete" OnClick="btn_Delete_Click" PreCallBackFunction="function (){return confirm('确定删除?');}" runat="server" ToolTip='<%#Container.ItemIndex%>'></anthem:LinkButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</anthem:Repeater>
<table width="90%" cellpadding="5" bgcolor="#e7ecce" border="1">
<tr>
<td align="center" colspan="2">
<anthem:Label ID="Txt_TableTitle" runat="server" AutoUpdateAfterCallBack="true" Text="添加新项"></anthem:Label>
<anthem:HiddenField ID="Txt_ItemIndex" runat="server" AutoUpdateAfterCallBack="true" />
</td>
</tr>
<tr>
<td align="right">
标题:
&nbsp;
</td>
<td align="left">
<anthem:TextBox ID="Txt_title" runat="server" AutoUpdateAfterCallBack="true" Width="301px"></anthem:TextBox></td>
</tr>
<tr>
<td align="right">
地址:
</td>
<td align="left">
<anthem:TextBox ID="Txt_url" runat="server" AutoUpdateAfterCallBack="true" Width="301px"></anthem:TextBox></td>
</tr>
<tr>
<td align="right">
图片地址
</td>
<td align="left">
<anthem:TextBox ID="Txt_name" runat="server" Width="303px" AutoUpdateAfterCallBack="true"></anthem:TextBox></td>
</tr>
<tr>
<td align="center" colspan="2">
<anthem:Button ID="Btn_Submit" runat="server" Text=" 确 定 " CssClass="button" OnClick="Btn_Submit_Click" AutoUpdateAfterCallBack="true" />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;
<anthem:Button ID="BtnBack" runat="server" Text=" 取 消 " CssClass="button" OnClick="BtnBack_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>

然后贴C# code

使用的是Anthem控件,不使用Anthem控件的朋友可以使用常规控件,不影响效果

代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.IO;

namespace test
{
public partial class shopWindowMar : System.Web.UI.Page
{
private static string ImgUrl = "/adv_pic";
private static string XmlUrl = "/xml/TheXml.xml";
private static DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ReadXmlInfo();
//读取XML文件并绑定到Repeater
}
}

//读取XML文件并绑定到Repeater
public void ReadXmlInfo()
{
ds.Tables.Clear();
ds.ReadXml(Server.MapPath(XmlUrl));
Repeater1.DataSource
= ds.Tables[0].DefaultView;
Repeater1.DataBind();
}
//从ds数据源重写XML文件
public void WriteXMLInfo()
{
ds.WriteXml(Server.MapPath(XmlUrl));
}
//删除某节点
protected void btn_Delete_Click(object sender, EventArgs e)
{
Anthem.LinkButton aButton
= sender as Anthem.LinkButton;
ds.Tables[
0].Rows.RemoveAt(int.Parse(aButton.ToolTip.ToString()));
WriteXMLInfo();
ReadXmlInfo();
}
//修改某节点,初始化修改表单数据
protected void btn_Edit_Click(object sender, EventArgs e)
{
Anthem.LinkButton aButton
= sender as Anthem.LinkButton;
int ItemIndex = int.Parse(aButton.ToolTip.ToString());
Txt_TableTitle.Text
= "修改现有第" + ItemIndex.ToString() + "个项目";
Btn_Submit.Text
= " 修 改 ";
Txt_title.Text
= ds.Tables[0].Rows[ItemIndex]["title"].ToString();
Txt_name.Text
= ds.Tables[0].Rows[ItemIndex]["name"].ToString();
Txt_url.Text
= ds.Tables[0].Rows[ItemIndex]["url"].ToString();
Txt_ItemIndex.Value
= ItemIndex.ToString();
}
//提交表单内容,进行新增或修改
protected void Btn_Submit_Click(object sender, EventArgs e)
{
if (Txt_ItemIndex.Value.ToString() != "")
{
int EditIndex = int.Parse(Txt_ItemIndex.Value.ToString());
ds.Tables[
0].Rows[EditIndex]["name"] = Txt_name.Text.ToString();
ds.Tables[
0].Rows[EditIndex]["title"] = Txt_title.Text.ToString();
ds.Tables[
0].Rows[EditIndex]["url"] = Txt_url.Text.ToString();
}
else
{
DataRow dr
= ds.Tables[0].NewRow();
dr[
"name"] = Txt_name.Text.ToString();
dr[
"title"] = Txt_title.Text.ToString();
dr[
"url"] = Txt_url.Text.ToString();
ds.Tables[
0].Rows.Add(dr);
}
WriteXMLInfo();
ReadXmlInfo();
initTxtTable();
}
//取消修改按钮事件
protected void BtnBack_Click(object sender, EventArgs e)
{
initTxtTable();
}
//重置表单内容
public void initTxtTable()
{
Txt_url.Text
= "";
Txt_title.Text
= "";
Txt_name.Text
= "";
Txt_ItemIndex.Value
= "";
Txt_TableTitle.Text
= "添加新项";
Btn_Submit.Text
= " 添 加 ";
}
}
}

转载自:http://www.cnblogs.com/JaggerLee/archive/2009/02/04/1384062.html

posted @ 2010-12-13 18:28  TerryChou  阅读(386)  评论(0编辑  收藏  举报