Considering that using dynamic pages on the homepage affects server performance, javascript is used to dynamically update the homepage.
Principle: Take press publication as an example. When a piece of news is sent in the background, an XML file is dynamically generated by accessing the database to save news information. When the front-end page is opened, use javascript to read and display XML files.
Background code:
SseEntities _ db = new sseEntities ();
Var newsToView = (from n in _ db. News select n). ToArray (). Reverse (). ToList ();
XmlDocument xd = new XmlDocument ();
XmlDeclaration xde;
Xde = xd. CreateXmlDeclaration ("1.0", "GBK", null );
Xd. AppendChild (xde );
XmlElement newslist = xd. CreateElement ("newslist ");
Xd. AppendChild (newslist );
XmlNode root = xd. SelectSingleNode ("newslist ");
XmlElement [] news = new XmlElement [4];
XmlElement [] text = new XmlElement [4];
XmlElement [] img = new XmlElement [4];
For (int I = 0; I <4; I ++)
{
News [I] = xd. CreateElement ("news ");
Text [I] = xd. CreateElement ("text ");
Img [I] = xd. CreateElement ("img ");
News [I]. SetAttribute ("id", I. ToString ());
If (I <newsToView. Count)
{
Text [I]. InnerText = newsToView [I]. Title;
Img [I]. InnerText = "Content/newsimages/slider_item1.png ";
}
Else
{
Text [I]. InnerText = "";
Img [I]. InnerText = "";
}
News [I]. AppendChild (text [I]);
News [I]. AppendChild (img [I]);
Root. AppendChild (news [I]);
}
Xd. Save (Server. MapPath ("../Sources/News. xml"); the front-end reads the Code:
<Script type = "text/javascript">
Function loadXML (xmlFile ){
Var xmlDoc;
If (! Window. ActiveXObject ){
Var parser = new DOMParser ();
XmlDoc = parser. parseFromString (xmlFile, "text/xml ");
} Else {
XmlDoc = new ActiveXObject ("Microsoft. XMLDOM ");
XmlDoc. async = "false ";
XmlDoc. load (xmlFile );
}
Return xmlDoc;
}
Var xmlDoc = loadXML ("Sources/News. xml ");
// Parse the xml file and determine whether an error occurs.
If (xmlDoc. parseError. errorCode! = 0 ){
Alert (xmlDoc. parseError. reason );
}
// Obtain the root node
Var nodes = xmlDoc.doc umentElement. childNodes;
</Script>
<Div id = "newsbody">
<Div id = "slider">
<Div class = "sliderItem" id = "slide1">
<Script type = "text/javascript">
Document. write ("Document. write (" ")
</Script>
</Div>
<! --/SliderItem -->
<Div class = "sliderItem" id = "slide2" style = "display: none">
<Script type = "text/javascript">
Document. write ("