Edit, insert, and delete data sources using Treeview and XML.

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.