不能用数据库中的Top,后面发现了用这样一个方法可以实现。。。现就这个方法总结写了一个页面。有兴趣的朋友们可以一起学习下。。。。
前台代码:
代码如下:
<%@ 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随机生成数据</title>
<script language="javascript" type="text/javascript" src="ajax/jquery.js"></script>
<script >
//页面的初始化
$(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)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center" id="randomData">
</div>
</form>
</body>
</html>
后台代码:
代码如下:
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>
///创建人:周昕
/// 创建时间:2009-6-9
/// 方法名称:InitData
/// 方法作用:动态的生成表格并随机的生成5条数据
/// </summary>
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("<table style='text-align:left;color:red;width:600px'><tr style='color:#00FF00;font-size:40px;text-align:center'><td colspan='3'>动态刷新用户信息</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();
}
}
效果图