1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| public partial class Default : System.Web.UI.Page
{
internal int selectIndex
{
get
{
if (this.Session["Page_selectIndex"] == null)
return -1;
return Int32.Parse(this.Session["Page_selectIndex"].ToString());
}
set
{
this.Session["Page_selectIndex"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
loadXmlData();
}
}
private void loadXmlData()
{
DataSet myDs = new DataSet();
myDs.ReadXml(Server.MapPath("fichier.xml"));
if (myDs.Tables.Count >0)
{
this.GridView1.DataSource = myDs;
this.GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
selectIndex = this.GridView1.SelectedIndex;
FindXmlData(selectIndex);
}
private void FindXmlData(int selectedIndex)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("fichier.xml"));
XmlNodeList xmlnodelist = xmldoc.DocumentElement.ChildNodes;
XmlNode xmlnode = xmlnodelist.Item(selectedIndex);
this.TextBox1.Text = xmlnode["title"].InnerText;
this.TextBox2.Text = xmlnode["link"].InnerText;
this.TextBox3.Text = xmlnode["description"].InnerText;
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("fichier.xml"));
XmlElement newelement = xmldoc.CreateElement("item");
XmlElement xmltitle = xmldoc.CreateElement("title");
XmlElement xmllink = xmldoc.CreateElement("link");
XmlElement xmldescription = xmldoc.CreateElement("description");
xmltitle.InnerText = this.TextBox1.Text.Trim();
xmllink.InnerText = this.TextBox2.Text.Trim();
xmldescription.InnerText = this.TextBox3.Text.Trim();
newelement.AppendChild(xmltitle);
newelement.AppendChild(xmllink);
newelement.AppendChild(xmldescription);
xmldoc.DocumentElement.AppendChild(newelement);
xmldoc.Save(Server.MapPath("fichier.xml"));
loadXmlData();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (selectIndex == -1)
{
this.RegisterClientScriptBlock("alertmessage", "<script>alert('please select one modify data item.')</script>");
}
else
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("fichier.xml"));
XmlNode xmlnode = xmldoc.DocumentElement.ChildNodes.Item(selectIndex);
xmlnode["title"].InnerText = this.TextBox1.Text.Trim();
xmlnode["link"].InnerText = this.TextBox2.Text.Trim();
xmlnode["description"].InnerText = this.TextBox3.Text.Trim();
xmldoc.Save(Server.MapPath("fichier.xml"));
loadXmlData();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("fichier.xml"));
XmlNode xmlnode = xmldoc.DocumentElement.ChildNodes.Item(selectIndex);
xmlnode.ParentNode.RemoveChild(xmlnode);
xmldoc.Save(Server.MapPath("fichier.xml"));
loadXmlData();
this.TextBox1.Text = "";
this.TextBox2.Text = "";
this.TextBox3.Text = "";
} |
Partager