ajax 數(shù)據(jù)庫中隨機(jī)讀取5條數(shù)據(jù)動(dòng)態(tài)在頁面中刷新
更新時(shí)間:2009年06月12日 19:32:34 作者:
以下是我在此編寫一個(gè)程序時(shí)的時(shí)刻所遇到的問題。因?yàn)橐婚_始經(jīng)理給我分配了要我寫一個(gè)在頁面上動(dòng)態(tài)的顯示5條數(shù)據(jù)。
不能用數(shù)據(jù)庫中的Top,后面發(fā)現(xiàn)了用這樣一個(gè)方法可以實(shí)現(xiàn)。?!,F(xiàn)就這個(gè)方法總結(jié)寫了一個(gè)頁面。有興趣的朋友們可以一起學(xué)習(xí)下。。。。
前臺(tái)代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajaxRandomData.aspx.cs" Inherits="ajaxRandomData" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ajax隨機(jī)生成數(shù)據(jù)</title>
<script language="javascript" type="text/javascript" src="ajax/jquery.js"></script>
<script >
//頁面的初始化
$(document).ready(function(){randomData()});
//頁面初始化函數(shù)
function randomData()
{
$.ajax({
type:'POST',
url:'ajaxRandomData.aspx',
data:{action:'randon'},
success:randomDatacallbace
});
}
// 頁面初始化回調(diào)函數(shù)
function randomDatacallbace(data)
{
if(data=="")
{
alert("暫時(shí)不能為您生成數(shù)據(jù)");
$("#randomData").html("暫時(shí)不能為您生成數(shù)據(jù)");
}
else
{
$("#randomData").html(data);
randomtime();//每隔5秒鐘執(zhí)行一次
}
}
//動(dòng)態(tài)變化頁面中顯示的數(shù)據(jù)。
function randomtime()
{
setTimeout(function(){randomData()},2000)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center" id="randomData">
</div>
</form>
</body>
</html>
后臺(tái)代碼:
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();
}
}
/// <summary>
///創(chuàng)建人:周昕
/// 創(chuàng)建時(shí)間:2009-6-9
/// 方法名稱:InitData
/// 方法作用:動(dòng)態(tài)的生成表格并隨機(jī)的生成5條數(shù)據(jù)
/// </summary>
public void InitData()
{
SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = ConfigurationManager.ConnectionStrings["BoBoConn"].ToString();
//隨機(jī)生成數(shù)據(jù)的關(guān)鍵
string sql = "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("<table style='text-align:left;color:red;width:600px'><tr style='color:#00FF00;font-size:40px;text-align:center'><td colspan='3'>動(dòng)態(tài)刷新用戶信息</td></tr><tr style='color:#6600FF'><td style='text-align:left;width:100px'>用戶名</td><td style='text-align:left;width:200px'>用戶全名</td><td style='width:250px'>電子油箱</td></tr>");
for (int i = 0; i < n; i++)
{
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("<tr><td>" + username + "</td>");
str.Append("<td>" + fullname + "</td>");
str.Append("<td>" + email + "</td></tr>");
}
else
{
str.Append("<tr style='color:blue'><td>" + username + "</td>");
str.Append("<td>" + fullname + "</td>");
str.Append("<td>" + email + "</td></tr>");
}
}
str.Append("</table>");
Response.Clear();
Response.ContentType = "application/text";
Response.Write(str);
Response.End();
}
}
效果圖

前臺(tái)代碼:
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajaxRandomData.aspx.cs" Inherits="ajaxRandomData" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ajax隨機(jī)生成數(shù)據(jù)</title>
<script language="javascript" type="text/javascript" src="ajax/jquery.js"></script>
<script >
//頁面的初始化
$(document).ready(function(){randomData()});
//頁面初始化函數(shù)
function randomData()
{
$.ajax({
type:'POST',
url:'ajaxRandomData.aspx',
data:{action:'randon'},
success:randomDatacallbace
});
}
// 頁面初始化回調(diào)函數(shù)
function randomDatacallbace(data)
{
if(data=="")
{
alert("暫時(shí)不能為您生成數(shù)據(jù)");
$("#randomData").html("暫時(shí)不能為您生成數(shù)據(jù)");
}
else
{
$("#randomData").html(data);
randomtime();//每隔5秒鐘執(zhí)行一次
}
}
//動(dòng)態(tài)變化頁面中顯示的數(shù)據(jù)。
function randomtime()
{
setTimeout(function(){randomData()},2000)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center" id="randomData">
</div>
</form>
</body>
</html>
后臺(tái)代碼:
復(fù)制代碼 代碼如下:
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();
}
}
/// <summary>
///創(chuàng)建人:周昕
/// 創(chuàng)建時(shí)間:2009-6-9
/// 方法名稱:InitData
/// 方法作用:動(dòng)態(tài)的生成表格并隨機(jī)的生成5條數(shù)據(jù)
/// </summary>
public void InitData()
{
SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = ConfigurationManager.ConnectionStrings["BoBoConn"].ToString();
//隨機(jī)生成數(shù)據(jù)的關(guān)鍵
string sql = "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("<table style='text-align:left;color:red;width:600px'><tr style='color:#00FF00;font-size:40px;text-align:center'><td colspan='3'>動(dòng)態(tài)刷新用戶信息</td></tr><tr style='color:#6600FF'><td style='text-align:left;width:100px'>用戶名</td><td style='text-align:left;width:200px'>用戶全名</td><td style='width:250px'>電子油箱</td></tr>");
for (int i = 0; i < n; i++)
{
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("<tr><td>" + username + "</td>");
str.Append("<td>" + fullname + "</td>");
str.Append("<td>" + email + "</td></tr>");
}
else
{
str.Append("<tr style='color:blue'><td>" + username + "</td>");
str.Append("<td>" + fullname + "</td>");
str.Append("<td>" + email + "</td></tr>");
}
}
str.Append("</table>");
Response.Clear();
Response.ContentType = "application/text";
Response.Write(str);
Response.End();
}
}
效果圖

相關(guān)文章
SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請求JSON格式數(shù)據(jù)
這篇文章主要介紹了SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請求JSON格式數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2016-05-05Ajax 向數(shù)據(jù)庫修改和添加功能(較簡答)
這篇文章主要介紹了Ajax 向數(shù)據(jù)庫修改和添加功能(較簡答),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03ajax從JSP傳遞對象數(shù)組到后臺(tái)的方法
今天小編就為大家分享一篇ajax從JSP傳遞對象數(shù)組到后臺(tái)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Ajax實(shí)現(xiàn)省市區(qū)三級級聯(lián)(數(shù)據(jù)來自mysql數(shù)據(jù)庫)
這篇文章主要為大家詳細(xì)介紹了Ajax實(shí)現(xiàn)省市區(qū)三級級聯(lián),數(shù)據(jù)來自mysql數(shù)據(jù)庫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09ajax實(shí)時(shí)任務(wù)提示功能的實(shí)現(xiàn)代碼
本項(xiàng)目運(yùn)用了 FLEAPHP,MYSQL,SMARTY,FCKEDItor,JSON,PROTOTYPE的技術(shù),在這里首先要感謝這些開源項(xiàng)目的開發(fā)者給我們帶來的好東西,其次要感謝[生氣豬--讓我?guī)退鲆粋€(gè)這樣的小東西來提醒她按時(shí)完成事情].花了一個(gè)3個(gè)小時(shí)完成.希望給大家起到拋磚引玉的作用啊....2008-09-09