ajax 随机,ajax 数据库中随机读取5条数据动态在页面中刷新

不能用数据库中的Top,后面发现了用这样一个方法可以实现。。。现就这个方法总结写了一个页面。有兴趣的朋友们可以一起学习下。。。。

前台代码:

ajax随机生成数据

//页面的初始化

$(document).ready(function(){randomData()});

//页面初始化函数

function randomData()

{

$.ajax({

type:'POST',

url:'ajaxRandomData.aspx',

data:{action:'randon'},

success:randomDatacallbace

});

}

// 页面初始化回调函数

function randomDatacallbace(data)

{

if(data=="")

{

alert("暂时不能为您生成数据");

$("#randomData").html("暂时不能为您生成数据");

}

else

{

$("#randomData").html(data);

randomtime();//每隔5秒钟执行一次

}

}

//动态变化页面中显示的数据。

function randomtime()

{

setTimeout(function(){randomData()},2000)

}

后台代码:

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();

}

}

///

///创建人:周昕

/// 创建时间:2009-6-9

/// 方法名称:InitData

/// 方法作用:动态的生成表格并随机的生成5条数据

///

public void InitData()

{

SqlConnection mycon = new SqlConnection();

mycon.ConnectionString = ConfigurationManager.ConnectionStrings["BoBoConn"].ToString();

//随机生成数据的关键

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("

动态刷新用户信息
用户名用户全名电子油箱

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("

" + 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();

}

}

效果图

20090612193228502.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值