Today, I wrote some code about Treeview and XML and made a small summary. For now, I only need to modify the Code. The Code above: I used Treeview and formview.
The first is the data binding method of formview.
Code
Protected void fvdatabind ()
{
String Path = treeview1.selectednode. datapath;
Int depth = treeview1.selectednode. depth;
Xmlperformance2.xpath = path;
Formview1.datasource = xmlperformance2;
Formview1.databind ();
} Code
Protected void treeview1_selectednodechanged (Object sender, eventargs E)
{
Fvdatabind ();
}
Protected void formviewincluitemcommand (Object sender, formviewcommandeventargs E)
{
Switch (E. commandname)
{
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 ");
Updatexml (server. mappath ("~ /Admin/hero/xmlfile2.xml "), TB. Text, tb1.text );
Break;
}
}
The preceding section binds formview to data when the tree node is selected, and displays the details of the selected node. The itemcommand method of formview is used to change the formview mode. Three modes are available: readonly, insert, and edit.
When the itemcommand method changes the formview mode, the modechanging event is triggered. The following code is used:
Code
Protected void formview1_modechanging (Object sender, formviewmodeeventargs E)
{
Switch (E. newmode)
{
Case formviewmode. Edit:
Fvdatabind ();
Break;
Case formviewmode. insert:
Fvdatabind ();
Break;
Case formviewmode. readonly:
Fvdatabind ();
Break;
}
}
Then write the updatexml method to edit the XML data source file. The Code is as follows:
Code
Protected void updatexml (string xmlpath, string newvalue, string newvalue1)
{
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 = "")
{
Xe. setattribute ("text", newvalue1 );
}
Else
{
Xe. setattribute ("type", newvalue );
}
Doc. Save (xmlpath );
}
After the update is completed, the formview mode is changed to readonly. The following is strange: I tried to trigger an event only in the itemupdating event. The Code is as follows:
Protected void formviewincluitemupdating (Object sender, formviewupdateeventargs E)
{
Formview1.changemode (formviewmode. readonly );
Fvdatabind ();
}
In the end, do not disable the viewstate of formview. Otherwise, the viewstate will be ineffective. Although the method is stupid, it will also become a functional requirement. Other operations will also be implemented and updated later, thank you!