web应用中如何实现使数据库里的数据在画面上每隔几秒自动切换? 请问在asp.net 2.0的web应用中,如何实现使数据库里的数据在画面上每隔3秒自动切换?比如有个单词表,画面启动时从中抽出5条数据,先显示第一个单词,然后过3秒显示它的意思。然后是下一个单词,依次类推,直到这5个单词和它的意思都显示完为止。如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 setTimeout(method,3000);method里调用刷新或者AJAX等. 看来还是得用Ajax,只用asp.net2.0 + javascript 能实现吗? setTimeout(method,3000); 能 试过setTimeout了,结果是页面一出来就只显示最后一个单词,能实现隔3秒显示。但是前面4个单词都没显示。发现setTimeout对循环不起作用。 可以呀,用什么ajax,又不是要不断的更新,先取去5条数据然后用js控制一下就是了 用timer控件也可以,不过是asp.net2.0才支持。或者直接写js代码:setTimeout(method,3000); method是你要调用服务器的js函数。或者直接定时刷新页面,当然最后一种方式比较野蛮。 已实现。谢谢大家,在这里贴上代码,然后结贴。就是不知道结了贴大家还能看到吗?.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default11.aspx.cs" Inherits="_Default11" %><!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>Word Recite</title> <link type="text/css" rel="stylesheet" href="~/Css/StyleSheet.css" /> <script language="javascript"> //添加javascript函数//// 让setTimeout能带参数//var _st = window.setTimeout; //window.setTimeout = function(fRef, mDelay)//{ // if(typeof fRef == 'function')// { // var argu = Array.prototype.slice.call(arguments,2); // var f = (function(){ fRef.apply(null, argu); }); // return _st(f, mDelay); // } // return _st(fRef,mDelay); //}/* 功能:修改 window.setTimeout,使之可以传递参数和对象参数 使用方法: setTimeout(回调函数,时间,参数1,,参数n)*/var __sto = setTimeout;window.setTimeout = function(callback,timeout,param){ var args = Array.prototype.slice.call(arguments,2); var _cb = function() { callback.apply(null,args); } __sto(_cb,timeout);}/////测试代码//function aaaa(a)//{// alert(a);//}//function aaaaa(a,b,c)//{// alert(a + b + c);//}//var a = new Object();//window.setTimeout(aaaa,1000,a);//window.setTimeout(aaaaa,2000,a,6,7); // 显示单词,页面加载时执行 function wordShow() { // 获取单词连接字符串 var objWord = document.getElementById("mLblWord"); var strWord = objWord.innerText; //alert(strWord); // 将单词连接字符串以#分隔成数组 var letter = strWord.split("#"); var wordCnt = letter.length; letter.shift(); // letter数组的第一个元素为空白,所以从第2个开始 // 显示单词 showLetter(letter); } // 显示单词 function showLetter(letter) { if(letter.length>0 ) { var timer0 = window.setTimeout(showLetter1,1000,letter); // 隔1秒显示第一个单词 } } // 显示第一个标签的字符串(单词) function showLetter1(letter) { if(document.getElementById("mLblDetail1") != null) { document.getElementById("mLblDetail1").innerText = letter.shift(); document.getElementById("mLblDetail2").innerText = ""; document.getElementById("mLblDetail3").innerText = ""; } var timer1 = window.setTimeout(showLetter2,2000,letter); // 隔2秒显示该单词的发音 } // 显示第二个标签的字符串(发音) function showLetter2(letter) { if(document.getElementById("mLblDetail2") != null) { document.getElementById("mLblDetail2").innerText = letter.shift(); document.getElementById("mLblDetail3").innerText = ""; } var timer2 = window.setTimeout(showLetter3,2000,letter); // 隔2秒显示该单词的意思 } // 显示第三个标签的字符串(意思) function showLetter3(letter) { if(document.getElementById("mLblDetail3") != null) { document.getElementById("mLblDetail3").innerText = letter.shift(); } if(letter.length == 0) // 没有单词了 { alert("辛苦了!单词背诵结束^^"); // 清空Timeout对象 window.clearTimeout(timer0); window.clearTimeout(timer1); window.clearTimeout(timer2); window.clearTimeout(timer3); // window.close(); //OK } var timer3 = window.setTimeout(showLetter1,2000,letter); // 隔2秒显示下一个单词 } </script></head><body style="position: static" onload="wordShow()"><%--<body style="position: static" >--%> <form id="form1" runat="server"> <div id="div1"></div> <div style="display:none"> <table style="width: 322px; height: 363px; text-align: center"> <tr> <td bgcolor="#ccffff" style="width: 75px; height: 34px; text-align: center"> 单词背诵</td> </tr> <tr> <td style="width: 75px; height: 146px"> <asp:ListBox ID="mListBox" runat="server" BackColor="#FFFFC0" Height="256px" OnSelectedIndexChanged="mListBox_SelectedIndexChanged" Width="672px" AutoPostBack="True"></asp:ListBox></td> </tr> <tr> <td style="width: 75px; height: 19px"> <asp:Label ID="mLblShow" runat="server" BackColor="#C0FFFF" Text="Label1" Width="668px"></asp:Label></td> </tr> </table> </div> <div style="background:#426ab3;" class="divCenter"> <table> <tr height="30px"></tr> <tr> <td style="width: 75px"> <asp:Label ID="mLblDetail1" runat="server" Text="" Width="500px" Font-Size=XX-Large ForeColor=white Font-Bold="true"></asp:Label></td> </tr> <tr height="30px"></tr> <tr> <td style="width: 75px"> <asp:Label ID="mLblDetail2" runat="server" Text="" Width="500px" Font-Size=XX-Large ForeColor=white Font-Bold="true"></asp:Label></td> </tr> <tr height="30px"></tr> <tr> <td style="width: 75px"> <asp:Label ID="mLblDetail3" runat="server" Text="" Width="500px" Font-Size=XX-Large ForeColor=white Font-Bold="true"></asp:Label></td> </tr> </table> </div> <div style = "display:none"> <asp:Label ID="mLblWord" runat="server" Text="" Width="662px"></asp:Label> </div> </form></body></html> .csusing System;using System.Data;using System.Configuration;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.Data.OracleClient;//using System.Threading; // For sleep (delay)public partial class _Default11 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //方法一:利用数据库 { //if (!this.IsPostBack) //{ OracleConnection con = DB.Connection(); con.Open(); OracleCommand cmd = new OracleCommand("select wordID, wordName from kout_WordTest", con); OracleDataReader dr = cmd.ExecuteReader(); mListBox.DataSource = dr; mListBox.DataTextField = "wordName"; mListBox.DataValueField = "wordID"; mListBox.DataBind(); dr.Close(); con.Close(); //int cntListBox = mListBox.Items.Count; //for (int i = 0; i < cntListBox; i++) //{ // mListBox.SelectedIndex = i; // if (i == 0) // { // mListBox_SelectedIndexChanged(sender, e); // ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>wordShow();</script>"); // } //} int cntListBox = mListBox.Items.Count; for (int i = 0; i < cntListBox; i++) { mListBox.SelectedIndex = i; mListBox_SelectedIndexChanged(sender, e); //if (i == 0) //{ // mListBox_SelectedIndexChanged(sender, e); // //Session["idx"] = i; // ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>wordShow();</script>"); //} //if (i == 1) //{ // mListBox_SelectedIndexChanged(sender, e); // //Session["idx"] = i; // ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>wordShow();</script>"); //} } //Thread.Sleep(2000); //mListBox_SelectedIndexChanged(sender, e); //} } ////方法二:随便写个数据 //{ // if (!this.IsPostBack) // { // mListBox.Items.Add("test"); // mListBox.Items.Add("mast"); // } //} } protected void mListBox_SelectedIndexChanged(object sender, EventArgs e) { //方法一:利用数据库 { int idx = mListBox.SelectedIndex + 1; //int idx = Convert.ToInt32(Session["idx"]); OracleConnection con = DB.Connection(); con.Open(); OracleCommand cmd = new OracleCommand("select wordName, wordRead, wordMean from kout_WordTest where wordID=" + idx, con); OracleDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { mLblShow.Text = "单词:" + dr.GetString(0) + " 读音:" + dr.GetString(1) + " 意思:" + dr.GetString(2); mLblWord.Text += "#" + dr.GetString(0) + "#" + dr.GetString(1) + "#" + dr.GetString(2); break; } dr.Close(); con.Close(); } ////方法二:随便写个数据 //{ // string value = mListBox.SelectedValue; // mLblDetail.Text = value; // mLblRead.Text = value + " 发音"; // mLblMean.Text = value + " 意思"; //} }}App_Code/DB.csusing System;using System.Data;using System.Configuration;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.Data.OracleClient;/// <summary>/// DB 的摘要说明/// </summary>public class DB{ public DB() { // // TODO: 在此处添加构造函数逻辑 // } // SQLServer //public static SqlConnection Connection() //{ // SqlConnection connection = new SqlConnection("server=.;database=WordTest;uid=sa;pwd=sa123"); // return connection; //} public static OracleConnection Connection() { OracleConnection connection = new OracleConnection("Data Source=OTCD1;Persist Security Info=True;User ID=otc;Password=novartis;Unicode=false"); return connection; } /* * create table kout_WordTest( WordID int not null, wordName varchar2(30) not null, wordRead varchar2(20) not null, wordMean varchar2(50) not null, primary key(wordID))/ * insert into kout_WordTest values(1, 'test', '[test]', 'CeShi,ShiYan');insert into kout_WordTest values(2, 'name', '[neim]', '名字,名称');insert into kout_WordTest values(3, 'word', '[wo:d]', 'DanCi,消息');insert into kout_WordTest values(4, 'self', '[self]', '自己,本人');insert into kout_WordTest values(5, 'cook', '[kuk] ', 'CuShi,PengTiao');insert into kout_WordTest values(6, 'good', '[god] ', 'Haoshi,LiYi');commit; * select * from kout_WordTest; */} 如何避免注册机批量注册会员 帮帮我哦 MVC beta 版本中 DropDownList 一个很急的问题,入门问题,谢谢 水晶报表的问题..没人会吗? 请问计数问题 保存成功后弹出对话框. 100分在线等:在线编辑器? 调查:有多少人用Infragistics NetAdvantage! 帮看看这个页面的AJax是怎么实现的? 关于React.js的状态同步问题 请问这样将表中的一列属性值依次读入一个TextBox中???? 使用window.close关闭页面时,怎么才能不出现提示框?
method里调用刷新或者AJAX等.
能
但是前面4个单词都没显示。发现setTimeout对循环不起作用。
就是不知道结了贴大家还能看到吗?.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default11.aspx.cs" Inherits="_Default11" %><!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>Word Recite</title>
<link type="text/css" rel="stylesheet" href="~/Css/StyleSheet.css" /> <script language="javascript"> //添加javascript函数//// 让setTimeout能带参数
//var _st = window.setTimeout;
//window.setTimeout = function(fRef, mDelay)
//{
// if(typeof fRef == 'function')
// {
// var argu = Array.prototype.slice.call(arguments,2);
// var f = (function(){ fRef.apply(null, argu); });
// return _st(f, mDelay);
// }
// return _st(fRef,mDelay);
//}/*
功能:修改 window.setTimeout,使之可以传递参数和对象参数
使用方法: setTimeout(回调函数,时间,参数1,,参数n)
*/
var __sto = setTimeout;
window.setTimeout = function(callback,timeout,param)
{
var args = Array.prototype.slice.call(arguments,2);
var _cb = function()
{
callback.apply(null,args);
}
__sto(_cb,timeout);
}/////测试代码//function aaaa(a)
//{
// alert(a);
//}//function aaaaa(a,b,c)
//{
// alert(a + b + c);
//}
//var a = new Object();
//window.setTimeout(aaaa,1000,a);
//window.setTimeout(aaaaa,2000,a,6,7); // 显示单词,页面加载时执行
function wordShow()
{
// 获取单词连接字符串
var objWord = document.getElementById("mLblWord");
var strWord = objWord.innerText;
//alert(strWord);
// 将单词连接字符串以#分隔成数组
var letter = strWord.split("#");
var wordCnt = letter.length;
letter.shift(); // letter数组的第一个元素为空白,所以从第2个开始
// 显示单词
showLetter(letter);
} // 显示单词
function showLetter(letter)
{
if(letter.length>0 )
{
var timer0 = window.setTimeout(showLetter1,1000,letter); // 隔1秒显示第一个单词
}
}
// 显示第一个标签的字符串(单词)
function showLetter1(letter)
{
if(document.getElementById("mLblDetail1") != null)
{
document.getElementById("mLblDetail1").innerText = letter.shift();
document.getElementById("mLblDetail2").innerText = "";
document.getElementById("mLblDetail3").innerText = "";
}
var timer1 = window.setTimeout(showLetter2,2000,letter); // 隔2秒显示该单词的发音
}
// 显示第二个标签的字符串(发音)
function showLetter2(letter)
{
if(document.getElementById("mLblDetail2") != null)
{
document.getElementById("mLblDetail2").innerText = letter.shift();
document.getElementById("mLblDetail3").innerText = "";
}
var timer2 = window.setTimeout(showLetter3,2000,letter); // 隔2秒显示该单词的意思
}
// 显示第三个标签的字符串(意思)
function showLetter3(letter)
{
if(document.getElementById("mLblDetail3") != null)
{
document.getElementById("mLblDetail3").innerText = letter.shift();
}
if(letter.length == 0) // 没有单词了
{
alert("辛苦了!单词背诵结束^^");
// 清空Timeout对象
window.clearTimeout(timer0);
window.clearTimeout(timer1);
window.clearTimeout(timer2);
window.clearTimeout(timer3);
// window.close(); //OK
}
var timer3 = window.setTimeout(showLetter1,2000,letter); // 隔2秒显示下一个单词
}
</script></head>
<body style="position: static" onload="wordShow()">
<%--<body style="position: static" >--%>
<form id="form1" runat="server">
<div id="div1"></div>
<div style="display:none">
<table style="width: 322px; height: 363px; text-align: center">
<tr>
<td bgcolor="#ccffff" style="width: 75px; height: 34px; text-align: center">
单词背诵</td>
</tr>
<tr>
<td style="width: 75px; height: 146px">
<asp:ListBox ID="mListBox" runat="server" BackColor="#FFFFC0" Height="256px" OnSelectedIndexChanged="mListBox_SelectedIndexChanged"
Width="672px" AutoPostBack="True"></asp:ListBox></td>
</tr>
<tr>
<td style="width: 75px; height: 19px">
<asp:Label ID="mLblShow" runat="server" BackColor="#C0FFFF" Text="Label1" Width="668px"></asp:Label></td>
</tr>
</table>
</div>
<div style="background:#426ab3;" class="divCenter">
<table>
<tr height="30px"></tr>
<tr>
<td style="width: 75px">
<asp:Label ID="mLblDetail1" runat="server" Text="" Width="500px" Font-Size=XX-Large ForeColor=white Font-Bold="true"></asp:Label></td>
</tr>
<tr height="30px"></tr>
<tr>
<td style="width: 75px">
<asp:Label ID="mLblDetail2" runat="server" Text="" Width="500px" Font-Size=XX-Large ForeColor=white Font-Bold="true"></asp:Label></td>
</tr>
<tr height="30px"></tr>
<tr>
<td style="width: 75px">
<asp:Label ID="mLblDetail3" runat="server" Text="" Width="500px" Font-Size=XX-Large ForeColor=white Font-Bold="true"></asp:Label></td>
</tr> </table>
</div>
<div style = "display:none">
<asp:Label ID="mLblWord" runat="server" Text="" Width="662px"></asp:Label>
</div>
</form>
</body>
</html>
using System.Data;
using System.Configuration;
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.Data.OracleClient;
//using System.Threading; // For sleep (delay)public partial class _Default11 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//方法一:利用数据库
{
//if (!this.IsPostBack)
//{
OracleConnection con = DB.Connection();
con.Open();
OracleCommand cmd = new OracleCommand("select wordID, wordName from kout_WordTest", con);
OracleDataReader dr = cmd.ExecuteReader();
mListBox.DataSource = dr;
mListBox.DataTextField = "wordName";
mListBox.DataValueField = "wordID";
mListBox.DataBind();
dr.Close();
con.Close(); //int cntListBox = mListBox.Items.Count;
//for (int i = 0; i < cntListBox; i++)
//{
// mListBox.SelectedIndex = i;
// if (i == 0)
// {
// mListBox_SelectedIndexChanged(sender, e);
// ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>wordShow();</script>");
// }
//} int cntListBox = mListBox.Items.Count;
for (int i = 0; i < cntListBox; i++)
{
mListBox.SelectedIndex = i;
mListBox_SelectedIndexChanged(sender, e);
//if (i == 0)
//{
// mListBox_SelectedIndexChanged(sender, e);
// //Session["idx"] = i;
// ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>wordShow();</script>");
//} //if (i == 1)
//{
// mListBox_SelectedIndexChanged(sender, e);
// //Session["idx"] = i;
// ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>wordShow();</script>");
//}
} //Thread.Sleep(2000);
//mListBox_SelectedIndexChanged(sender, e);
//}
} ////方法二:随便写个数据
//{
// if (!this.IsPostBack)
// {
// mListBox.Items.Add("test");
// mListBox.Items.Add("mast");
// }
//}
} protected void mListBox_SelectedIndexChanged(object sender, EventArgs e)
{
//方法一:利用数据库
{
int idx = mListBox.SelectedIndex + 1;
//int idx = Convert.ToInt32(Session["idx"]);
OracleConnection con = DB.Connection();
con.Open();
OracleCommand cmd = new OracleCommand("select wordName, wordRead, wordMean from kout_WordTest where wordID=" + idx, con);
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
mLblShow.Text = "单词:" + dr.GetString(0) + " 读音:" + dr.GetString(1) + " 意思:" + dr.GetString(2);
mLblWord.Text += "#" + dr.GetString(0) + "#" + dr.GetString(1) + "#" + dr.GetString(2);
break;
}
dr.Close();
con.Close();
} ////方法二:随便写个数据
//{
// string value = mListBox.SelectedValue;
// mLblDetail.Text = value;
// mLblRead.Text = value + " 发音";
// mLblMean.Text = value + " 意思";
//}
}
}App_Code/DB.csusing System;
using System.Data;
using System.Configuration;
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.Data.OracleClient;/// <summary>
/// DB 的摘要说明
/// </summary>
public class DB
{
public DB()
{
//
// TODO: 在此处添加构造函数逻辑
//
} // SQLServer
//public static SqlConnection Connection()
//{
// SqlConnection connection = new SqlConnection("server=.;database=WordTest;uid=sa;pwd=sa123");
// return connection;
//} public static OracleConnection Connection()
{
OracleConnection connection = new OracleConnection("Data Source=OTCD1;Persist Security Info=True;User ID=otc;Password=novartis;Unicode=false");
return connection;
} /*
*
create table kout_WordTest
(
WordID int not null,
wordName varchar2(30) not null,
wordRead varchar2(20) not null,
wordMean varchar2(50) not null,
primary key(wordID)
)
/
*
insert into kout_WordTest values(1, 'test', '[test]', 'CeShi,ShiYan');
insert into kout_WordTest values(2, 'name', '[neim]', '名字,名称');
insert into kout_WordTest values(3, 'word', '[wo:d]', 'DanCi,消息');
insert into kout_WordTest values(4, 'self', '[self]', '自己,本人');
insert into kout_WordTest values(5, 'cook', '[kuk] ', 'CuShi,PengTiao');
insert into kout_WordTest values(6, 'good', '[god] ', 'Haoshi,LiYi');
commit;
*
select * from kout_WordTest;
*/
}