How to use ajax to realize the dynamic refresh of 5 pieces of data randomly read in the database in the page
This article mainly introduces how to use ajax to achieve the database to read 5 pieces of data dynamically refresh in the page, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Foreground code:
Ajax randomly generates data / / page initialization $(document) .ready (function () {randomData ()}); / / page initialization function function randomData () {$.ajax ({type:'POST',url:'ajaxRandomData.aspx',data: {action:'randon'}, success:randomDatacallbace});} / / Page initialization callback function function randomDatacallbace (data) {if (data== ") {alert (" data cannot be generated for you for the time being ") $("# randomData"). Html ("cannot generate data for you temporarily");} else {$("# randomData") .html (data); randomtime (); / / execute}} / / dynamically change the data displayed on the page every 5 seconds. Function randomtime () {setTimeout (function () {randomData ()}, 2000)}
Background code:
Using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;using System.Text;public partial class ajaxRandomData: System.Web.UI.Page {string StrAction= "; protected void Page_Load (object sender, EventArgs e) {StrAction=Request [" action "]; if (StrAction==" randon ") {InitData () } / founder: Zhou Xin / creation time: 2009-6-9 shock / method name: InitData/// method function: dynamically generate tables and randomly generate 5 data / public void InitData () {SqlConnection mycon = new SqlConnection (); mycon.ConnectionString = ConfigurationManager.ConnectionStrings ["BoBoConn"]. ToString (); / / key string sql for randomly generated data = "select top 5 *, newid () from loginuser order by newid ()" SqlDataAdapter myda = new SqlDataAdapter (sql, mycon); DataSet myset = new DataSet (); myda.Fill (myset); DataTable mytable = myset.Tables [0]; int n = mytable.Rows.Count;StringBuilder str = new StringBuilder (); str.Append ("dynamically refresh user name user name electronic fuel tank"); for (int I = 0; I < n; itank +) {string username = mytable.Rows [I] ["username"] .ToString (); string fullname = mytable.Rows [I] ["FullName"]. ToString () String email = mytable.Rows [I] ["Email"] .ToString (); if (I% 2! = 0) {str.Append ("+ username +"); str.Append ("" + fullname + "); str.Append (" + email + ");} else {str.Append ("+ username +"); str.Append (" + fullname + "); str.Append ("+ email +");} str.Append ("); Response.Clear () Response.ContentType = "application/text"; Response.Write (str); Response.End ();}} Thank you for reading this article carefully. I hope the article "how to use ajax to dynamically refresh 5 pieces of data in the database" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!