Read-write XML control of ASP. NET MVC Background Operation homepage Dynamic page switch display

Source: Internet
Author: User

I. BACKGROUND

In the ASP. NET MVC project, users need to open up several active layouts and present them on the right side of the home page in the sidebar, with inconsistent activity times to avoid visitors entering the Open service page when the activity is not open. Therefore not only need to add the restriction function in the activity code, also need to cancel the outdated page display in the foreground. Background:

Second, the problem analysis

To control the page switch display, you have to have the media to store the switch information, the operation will be recorded in the background, and when the browser loading the homepage can read the operation record and control the page switch. Therefore, consider two kinds of media storage switch record information, one is to use the database MySQL, and the second is XML. Considering that the database is overqualified for functionality and will increase the consumption of the database resources when the page loads, it is better to consider using an XML document to record the switch record. Here we need to do a preliminary work, first design the structure of XML:

  

<?XML version= "1.0" encoding= "gb2312"?><Settings>  <SettingID= "1">    <title>****</title>    <Status>True</Status>  </Setting></Settings>

The description is: ID is the setting of multiple records, the title is the name of the settings page, that is, the name of the service, status indicates the current service page status, there are two values: TRUE or FALSE. Add a folder named XML to the project and save the design XML named servicesettings.xml.

Third, solve the idea or process

  first of all , the switch operation requires the control on the page, the checkbox tag used here, of course, this is not the only choice, friends can use other tags as input control. The idea is: I hit the checkbox, the corresponding JS code record value, and the Ajax upload to the background action, the background action to respond.

JS Code:

function Bloodcontroller () {        $.ajax ({            "POST",            "/admin/controller",            data: {},            "Json"        });    

HTML code:

<name= "Switch-field-1"  class= "Ace Ace-switch Ace-switch-6 "  type=" checkbox "  onclick=" Controller () "/> 

The data in Ajax is not marked here. Triggers the background action once, then runs the code of the action XML in the action, then changes the value recorded in the XML.

Then, it is the implementation of the action XML in the action. The idea is to get the relative position of the XML in the application and then change the value of the XML.

 Public string " /xml/servicesettings.xml " ;                  Public actionresult Controller ()        {            new  operatexml ();            Sx.modifyxml (Xmlpath);             return Json ("");        }

Description: HttpRuntime.AppDomainAppPath.ToString (); method to get the root directory where the current application resides. In order to manipulate multiple XML files, I wrote Operatexml A class method, which goes into the operation details of XML.

The principle of XML operation is not much to say, in fact, ADO support is also very good, the structure of the XML document is similar to HTML, but it has a higher degree of freedom, can be labeled memory is also higher. Not much to say, directly on the code, the comments are explained:

 Public  voidModifyxml (stringPath) {XmlDocument xmldoc=NewXmlDocument ();            Xmldoc.load (Path); XmlNodeList NodeList= Xmldoc.selectsinglenode ("Settings"). ChildNodes;//get all child nodes of the bookstore node            foreach(XmlNode xninchNodeList)//Traverse all child nodes{XmlElement XE= (XmlElement) xn;//To Convert a child node type to a XmlElement type                if(XE. GetAttribute ("ID") =="1") {XmlNodeList NLS= Xe. ChildNodes;//continue to acquire all child nodes of the XE child node                    foreach(XmlNode xn1inchNls//Traverse{XmlElement Xe2= (XmlElement) xn1;//Conversion Type                        if(Xe2. Name = ="Status")//If you find                        {                            if(Xe2. InnerText! ="false") {Xe2. InnerText="false";//then modify                                 Break;//we can find the exit .                            }                            Else if(Xe2. InnerText = ="false") {Xe2. InnerText="true";//then modify                                 Break;//we can find the exit .                            }                        }                    }                     Break; }} xmldoc.save (Path);//saved. }

Because the code comment is clear enough to explain, do not repeat here, it should be noted that: the status here is only two values: TRUE or false; The two values are read at the foreground for page control.

Next, the foreground reads the value in the XML. The idea is: Read the XML value in the dynamic page, then make a judgment, true displays the page, False does not display the page. Read the XML code here, and explain the details in the comments. Need to explain is: here in order to control the display of multiple pages, using the list<string> to reprint multiple status values.

 Publiclist<string> Getallxml (stringPath) {            stringStatus =""; XmlDocument Doc=NewXmlDocument (); Doc.            Load (Path); XmlNode xn= Doc. selectSingleNode ("Settings"); XmlNodeList XNL=NULL; XNL= xn. SelectNodes ("/settings/setting/status"); List<string> list =Newlist<string>(); foreach(XmlNode xn1inchxnl) {Status=xn1.                InnerText; List.            ADD (status); }            returnlist; }

need to explain is: here in order to control the display of multiple pages, using the list<string> to reprint multiple status values. This is done on the page in the foreground page:

@using theone.tool@{operatexml SX=NewOperatexml (); List<string> list =Newlist<string>(); stringPath = HttpRuntime.AppDomainAppPath.ToString () +"/xml/quickservicesettings.xml"; List=sx.getallxml (path); stringStatus_umbrella = list[0].    ToString (); //string status_bottle = List[1].    ToString (); //string status_older = List[2].    ToString (); //string status_blood = List[3]. ToString ();} @if (Status_umbrella=="true"){    <divclass="Blog-grids Wow Fadeindown"data-wow-duration=". 8s"data-wow-delay=". 2s"> <divclass="Blog-grid-left"> <a href="/umbrellamanage/createumbrellauser">"/content/frontend/images/yusan_2.jpg" class="img-responsive"alt=""></a> </div> <divclass="Blog-grid-right"> "/umbrellamanage/createumbrellauser"> Borrow umbrellas </a> class="Clearfix"> </div> </div>}Else if(Status_umbrella = ="false"){}

Here, use the XML control page switch to introduce the basic end.

Iv. Summary

The core of the idea is the manipulation of XML, the modification of XML and the reading of XML. And for how to implement the checkbox state switch, this implementation can use JS to operate, read the status value, and let the corresponding JS code control checkbox is selected or unchecked. Actual Project Demo address : Click on the South Agricultural and industrial Red Council

If my thoughts and methods are wrong, I implore you to correct me, and humbly ask for advice. Please indicate the source and origin, thank you for your cooperation by the point will not be

Read-write XML control of ASP. NET MVC Background Operation homepage Dynamic page switch display

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.