[Repost] Using ASP.NET to implement XML-based message board

xiaoxiao2021-03-06  18

One. summary:

The message board is an important part of the website. It is the place where visitors express opinions are also a powerful tool for website administrators to understand the basic situation of the website. So the message board played an important role in the current website.

However, in the previous development of a message book is not an easy task, the developer's work will teach a lot. And now with Microsoft launches vs.net, the corresponding technology is new. In particular, XML is widely used in .NET Framework, the entire .NET architecture has a very superior foundation. At the same time, it is also accompanied by a emerging language C #. C # As an important part of Microsoft .NET strategy, it has special excellent performance. So the ASP.NET program in this article is described in C # language, while the entire program is based on XML. I used XML as a database of the program, mainly because the server is supported by XML files.

two. Claim:

(1) .NET SDK BETA2 and later

(2) Support for ASP.NET web server

three. Description:

The examples of this article consist of two parts:

(1) GuestPost.aspx - Add user information to an XML file

(2) ViewGuestbook.aspx - create a dataset object first. After the establishment, it is easy to display the data. I used a "repeater" to display data in the data set. Also, the reader can try to modify the viewpage based on your own preference.

four. Code:

(1) GuestPost.aspx:

<% @ Page language = "c #" EnableSessionState = "false"%> <% @ import namespace = "system"%> <% @ Import namespace = "system.io"%> <% @ Import namespace = "system.data "%> <% - these are the namespaces that must be used in this program -%>

Welcome to my message Bo </ title> <script language = "c #" runat = "server"> /// <summary> // When submitting (SUBMIT) button Press After that, the quarry /// </ summary> public void submit_click (Object sender, Eventargs e) {// Save the data of the XML file of the data // If your path is different, please modify String DataFile = "db / guest.xml";</p> <p>// Use a try-catch block to complete the information add function try {// only handles it if the page is valid.</p> <p>ErrMess.Text = ""; // Open a filestream in read mode to access database filestream fin; FIN = New filestream (server.mappath (datafile), filemode.open, fileaccess.read, fileshare.readwrite; // Establishment A database object DataSet GuestData = new dataset (); // only read XML SchemaguestData.readxml SchemaguestData.readxmlschema (FIN) only from the database; Fin.close (); // New Data line DataRow newrow = GuestData.Tables from the data set 0] .newrow (); // fill in the data line with the corresponding value ["name"] = name.text; newrow ["country"] = country.text; newrow ["email"] = email.text; newrow [ "Comments"] = comments.text; newrow ["datetime"] = datetime.now.tostring (); // Add the data line to the Data Set GuestData.Tables [0] .Rows.Add (newrow); // Newly build another write mode for the database file, save the file filestream fout; fout = new filestream (server.mappath (datafile), filemode.open, fileaccess.write, fileshare.readwrite;</p> <p>GuestData.writeXml (Fout, XMLWRITE.WRITESCHEMA); fout.close (); // Hide current panel FormPanel.visible = false; // Show a panel with thank you information ThankPanel.visible = true;}} catch (Exception EDD) {// Capture anomaly errmess.text = "Write XML file error, Cause:" edd.toString ();}} </ script> <link href = "myStyle.css" type = text / css rel = stylesheet > </ head> <body> <% - Contains a header file: header.inc -%> <! - #include file = "header.inc" -> <br> <h3 align = "center" Class = "newsbody"> Messagers Information </ h3> <br> <asp: label id = "errmess" text = "" Style = "color: # ff0000" runat = "server" /> <ask: panel id = FormPanel Runat = Server> <form runat = "server"> <table border = "0" width = "80%" align = "center"> <tr> <td class = "newsheading"> <b> Please leave a message Leave your valuable information! ! </ b> </ td> <td class = "newsheading"> </ td> </ tr> <TR class = "newsbody"> <td> Name: </ td> <td> <asp: textbox text = "" id = "name" runat = "server" /> <ask: requiredate = name display = staticRunat = server> * </ ask: RequiredFieldValidator> </ td> </ tr> <trs = "newsbody"> <TD> Country: </ TD> <TD> <asp: textbox text = "" id = "country" runat = "server" /> <ask: requiredfieldvalidator control channelovalidate = country display = staticRunat = Server> * </ asp: RequiredFieldValidator></p> <p></ td> </ tr> <trs = "newsbody"> <td> e-mail: </ td> <td> <asp: textbox test = "" "" id = "email" runat = "server" /> <asp: RequiredFieldValidator ControlToValidate = Email display = staticrunat = server> * </ asp: RequiredFieldValidator> <asp: RegularExpressionValidatorrunat = "server" ControlToValidate = "Email" ValidationExpression = "[/ w -] @ ([/ w -] /.) [/w-] "display="static "font-name="verdana" font-size = "10pt"> Please enter a correct email address! </ asp: regularExpressionValidator> </ td> </ tr> <TD> message: </ td> message: </ td> <TD> <ask: textBox textmode = multiline id = "color" columns = "25 "ROWS =" 4 "runat =" server "/> </ td> </ tr> <tr class =" newsbody "> <td color copan =" 2 "> <asp: button class =" newsheading "id =" Write "Text =" submit "onclick =" submit_click "runat =" server "/> </ td> </ tr> </ table> </ form> </ asp: panel> <ask: panel id = ThankPanel Visible = false Runat = Server> <p class = "newsbody" align = center> <b> Thank you for accessing my message board! </ b> <br> <a href = "viewguestbook.aspx"> Please click here </ a> to view the message book. </ p> </ asp: panel> <! - #Clude file = "footer.inc" -> </ body> </ html></p> <p>(2) ViewGuestbook.aspx:</p> <p><% @ Page language = "c #"%> <% @ import namespace = "system"%> <% @ import namespace = "system.io"%> <% @ import namespace = "system.data"%> <% - The above is the required name space -%> <html> <head> <title> Welcome to my message Bo </ title> <script language = "c #" runat = server> // page download , Run this script public void page_load (Object sender, eventargs e) {// The path to the XML file containing all data // If your path is different, please modify String DataFile = "db / guest.xml";</p> <p>// Use a try-catch block completion information reading function try {// created a dataset object Dataset GuestData = new dataset (); // Open a filestreamFileStream Fin for the database file; FIN = New FileStream (Server.MAppath (DataFile ), Filemode.open, fileaccess.read, fileshare.readwrite; // read the contents of the database to the data set GuestData.Readxml (FIN); Fin.Close (); // Pave the data in the first table Give RepeaterMyDataList.dataSource = guestdata.tables [0] .default (); mydatalist.database ();} catch (exception edd) {// capture exception errmess.text = "You cannot read data from XML files, why:" EDD. ToString ();}} </ script> <link href = "MyStyle.css" type = text / css rel = stylesheet> </ head> <body TopMargin = "0" leftmargin = "0" marginwidth = "0" marginheight = "0" Right Margin = "0"> <! - #nclude file = "header.inc" -> <ask: label id = "errmess" text = "" style = "color: # ff0000" runat = " Server "/> <br> <h3 align =" center "class =" newsbody "> My message book </ h3> <ask: repeater id =" mydatalist "runat =" server "></p> <p><headertemplate> <table class = "mainheads" width = "100%" style = "font: 8pt Verdana"> <tr style = "Background-color: # ff9966> <tH> Name </ tH> <TH> country </ TH> <TH> Email </ TH> <TH> Message </ TH> <TH> Date / Time </ TH> <{"<{"> <TR Style = "Background-Color: #Fffccc "> <TD> <% # databinder.eval (container.dataitem," name ")%> </ td> <td> <% # databinder.eval (container.DataItem," country ")%> </ TD> <TD> <% # databinder.eval ("email")%> </ td> <td> <data # databinder.eval (container.DataItem, "comments")%> </ td> <TD> <% # DataBinder.eval (Container.DataItem, "DateTime")%> </ td> </ tr> </ itemtemplate></p> <p><footertemplate> </ table> </ footertemplate> </ asp: repeater></p> <p><! - #nclude file = "footer.inc" -> </ body> </ html></p> <p>Fives. to sum up:</p> <p>This program is completed, I believe that everyone has a general understanding of the principles and skills. Through this article, I mainly want to show you the advantage of handling some database issues with XML: The server supports XML files, and the processing process is quite concise. Of course, there is also its shortcomings with XML, that is, when the database is large, the resolution process will cost a long time, so there is still a large database system. So, I just want to play a role in throwing jade from this article.</p> <p>The illustration after the program is running as follows:</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-41908.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="41908" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.035</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'vE2spGVLydd2uqGKbrrtDTjAavFxFw2iTqfSFcaSjy4wdSVvOgCHrTwKozTk5jHndEEoNKcNDXY9vM9a'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>