我写了一个用户控件,页面调用多次,但是总是只能正常显示第一个,帮我找找一下错误.
WebForm1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication7.WebForm1" %>
<%@ Register src="re_Seckill.ascx" tagname="re_Seckill" tagprefix="uc1" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div><uc1:re_Seckill ID="re_Seckill1" runat="server" StartSeckill="2012-09-29 23:05:12" EndSeckill="2012-09-29 23:12:32" /></div>
<div><uc1:re_Seckill ID="re_Seckill2" runat="server" StartSeckill="2012-09-29 23:05:12" EndSeckill="2012-09-29 23:12:32" /></div>
</div>
</form>
</body>
</html>
re_Seckill.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="re_Seckill.ascx.cs" Inherits="WebApplication7.re_Seckill" %>
<style type="text/css">
.style1
{
color:#FF0000;
font-weight:bold;
padding-left:7px;
padding-right:7px;
}
</style>
<script language="JavaScript" type="text/javascript">
function showTimeLimit(str, end, i) {
var now = new Date(str);
var future = new Date();
future -= 1000;
if (now - future > 0) {
var days = (now - future) / 1000 / 60 / 60 / 24;
var dayNum = Math.floor(days);
var hours = (now - future) / 1000 / 60 / 60 - (24 * dayNum);
var houNum = Math.floor(hours);
if (houNum < 10) {
houNum = "0" + houNum;
}
var minutes = (now - future) / 1000 / 60 - (24 * 60 * dayNum) - (60 * houNum);
var minNum = Math.floor(minutes);
if (minNum < 10) {
minNum = "0" + minNum;
}
var seconds = (now - future) / 1000 - (24 * 60 * 60 * dayNum) - (60 * 60 * houNum) - (60 * minNum);
var secNum = Math.floor(seconds);
if (secNum < 10) {
secNum = "0" + secNum;
}
document.getElementsByName('TimeLimit1')[i].innerHTML = "距秒杀开始还有:<span class=\"style1\">" + dayNum + "</span>天<span class=\"style1\">" + houNum + "</span>小时<span class=\"style1\">" + minNum + "</span>分<span class=\"style1\">" + secNum + "</span>秒";
}
else {
// setInterval('showTimeLimit(\"2012/09/29 09:11:50\",\"0\")', 1000);
var now = new Date(end);
var future = new Date();
future -= 1000;
if (now - future > 0) {
var days = (now - future) / 1000 / 60 / 60 / 24;
var dayNum = Math.floor(days);
var hours = (now - future) / 1000 / 60 / 60 - (24 * dayNum);
var houNum = Math.floor(hours);
if (houNum < 10) {
houNum = "0" + houNum;
}
var minutes = (now - future) / 1000 / 60 - (24 * 60 * dayNum) - (60 * houNum);
var minNum = Math.floor(minutes);
if (minNum < 10) {
minNum = "0" + minNum;
}
var seconds = (now - future) / 1000 - (24 * 60 * 60 * dayNum) - (60 * 60 * houNum) - (60 * minNum);
var secNum = Math.floor(seconds);
if (secNum < 10) {
secNum = "0" + secNum;
}
document.getElementsByName('TimeLimit1')[i].innerHTML = "距秒杀结束还有:<span class=\"style1\">" + dayNum + "</span>天<span class=\"style1\">" + houNum + "</span>小时<span class=\"style1\">" + minNum + "</span>分<span class=\"style1\">" + secNum + "</span>秒";
}
else {
document.getElementsByName('TimeLimit1')[i].innerHTML = "<span clas=\"style1\">秒杀已结束</span>";
}
}
}
</script>
<span id="TimeLimit1" name="TimeLimit1">请稍等,正在载入中。。。</span>
<asp:Literal runat="server" ID="lblTime1"></asp:Literal>
re_Seckill.ascx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class re_Seckill : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
this.lblTime1.Text = "<script language=\"javascript\" type=\"text/javascript\">setInterval('showTimeLimit(\"" + StartSeckill + "\",\"" + EndSeckill + "\",\"0\")', 1000);</script>";
}
public DateTime StartSeckill
{
get
{
EnsureChildControls();
return (DateTime)(ViewState["start time"] ?? DateTime.Now);
}
set
{
EnsureChildControls();
ViewState["start time"] = value;
}
}
public DateTime EndSeckill
{
get
{
EnsureChildControls();
return (DateTime)(ViewState["end time"] ?? DateTime.Now);
}
set
{
EnsureChildControls();
ViewState["end time"] = value;
}
}
}
}
[解决办法]
你这些js的函数名、变量都是相同的,会相互覆盖的,所以说不行的,明白道理了吗。你应该采用闭包或者加上控件的ClientID才可以
另外, document.getElementsByName('TimeLimit1')
<span id="TimeLimit1" name="TimeLimit1">
对非表单控件是无效的