更新后直接上代码:
Code
protected void fvDataBind()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (TreeView1.SelectedNode != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string path = TreeView1.SelectedNode.DataPath;
int depth = TreeView1.SelectedNode.Depth;
XmlDataSource2.XPath = path;
FormView1.DataSource = XmlDataSource2;
FormView1.DataBind();
Label1.Text = "";
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Label1.Text = "请选择你要编辑的节点位置";
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
fvDataBind();
}
protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
switch (e.CommandName)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "Edit":
FormView1.ChangeMode(FormViewMode.Edit);
break;
case "New":
FormView1.ChangeMode(FormViewMode.Insert);
break;
case "Cancel":
FormView1.ChangeMode(FormViewMode.ReadOnly);
break;
case "Update":
TextBox tb = (TextBox)this.FormView1.FindControl("TextBox1");
TextBox tb1 = (TextBox)this.FormView1.FindControl("TextBox2");
TextBox tb4=(TextBox)this.FormView1.FindControl("TextBox4");
updateXml(Server.MapPath("~/admin/hero/XMLFile2.xml"), tb.Text, tb1.Text,tb4.Text);
break;
case "Insert":
TextBox tb5 = (TextBox)this.FormView1.FindControl("TextBox5");
TextBox tb6= (TextBox)this.FormView1.FindControl("TextBox6");
TextBox tb3 = (TextBox)this.FormView1.FindControl("TextBox3");
RadioButton rb1 = (RadioButton)this.FormView1.FindControl("RadioButton1");
RadioButton rb2 = (RadioButton)this.FormView1.FindControl("RadioButton2");
if (rb2.Checked)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
insertXml(Server.MapPath("~/admin/hero/XMLFile2.xml"), tb5.Text, tb6.Text, tb3.Text, "before");
}
if (rb1.Checked)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
insertXml(Server.MapPath("~/admin/hero/XMLFile2.xml"), tb5.Text, tb6.Text, tb3.Text, "after");
}
break;
case "Delete":
deleteXml(Server.MapPath("~/admin/hero/XMLFile2.xml"));
break;
}
}
protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
switch(e.NewMode )
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case FormViewMode.Edit:
fvDataBind();
break;
case FormViewMode.Insert:
fvDataBind();
break;
case FormViewMode.ReadOnly:
fvDataBind();
break;
}
}
protected void updateXml(string xmlpath,string newvalue,string newvalue1,string navurl)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlpath);
XmlElement root = doc.DocumentElement;
string xpath = TreeView1.SelectedNode.DataPath;
XmlNode xn = root.SelectSingleNode(xpath);
XmlElement xe = (XmlElement)xn;
if (newvalue == "")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xe.SetAttribute("text", newvalue1);
xe.SetAttribute("navurl", navurl);
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xe.SetAttribute("type", newvalue);
xe.SetAttribute("navurl", navurl);
}
doc.Save(xmlpath);
}
protected void insertXml(string xmlpath,string newvalue,string newvalue1,string navurl,string position)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlpath);
XmlElement root = doc.DocumentElement;
string xpath = TreeView1.SelectedNode.DataPath;
int depth=TreeView1.SelectedNode.Depth;
XmlNode xn = root.SelectSingleNode(xpath);
XmlElement xe = (XmlElement)xn;
if (newvalue == "")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
XmlElement xe1 = doc.CreateElement("Prpducttype");
xe1.SetAttribute("text", newvalue1);
xe1.SetAttribute("navurl", navurl);
if (depth == 1)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (position == "before")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xe.ParentNode.InsertBefore(xe1, xn);
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xe.ParentNode.InsertAfter(xe1, xn);
}
}
if (depth == 0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (position == "before")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Label1.Text = "不能插入";
}
if (position == "after")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xe.AppendChild(xe1);
}
}
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
XmlElement xe1 = doc.CreateElement("Product");
xe1.SetAttribute("type", newvalue);
xe1.SetAttribute("navurl", navurl);
if (position == "before")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xe.ParentNode.InsertBefore(xe1, xn);
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xe.ParentNode.InsertAfter(xe1, xn);
}
}
doc.Save(xmlpath);
}
protected void deleteXml(string xmlpath)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlpath);
XmlElement root = doc.DocumentElement;
string xpath = TreeView1.SelectedNode.DataPath;
int depth = TreeView1.SelectedNode.Depth;
XmlNode xn = root.SelectSingleNode(xpath);
XmlElement xe = (XmlElement)xn;
xe.ParentNode.RemoveChild(xn);
doc.Save(xmlpath);
}
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
FormView1.ChangeMode(FormViewMode.ReadOnly);
TreeView1.DataBind();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
FormView1.ChangeMode(FormViewMode.ReadOnly);
TreeView1.DataBind();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void FormView1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
FormView1.ChangeMode(FormViewMode.ReadOnly);
TreeView1.DataBind();
}
写完发现自己还是没有按面向对象来写代码,纯粹是面向过程和功能,缺点很明显,只能修改父级和子级,多了没那功能,不过总算完成点需要,就先这样,再学习吧。