我写了一个用户控件,页面调用多次,但是总是只能正常显示第一个,帮我找找一下错误.

jisuanjizhuan 2012-10-01 09:03:52
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;
}
}
}
}
...全文
172 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jisuanjizhuan 2012-11-06
  • 打赏
  • 举报
回复
我发现这样获取的是客户端控件,要想获取服务器端时间后台得再向前台传当前时间的值,但我这样做了,倒计时不走了。 用户控件前台: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Seckill.ascx.cs" Inherits="WebApplication7.Seckill" %> <style type="text/css"> .style1 { color:#FF0000; font-weight:bold; } .style2 { color:#FF0000; font-size:x-large; padding-left:7px; padding-right:7px; } </style> <script language="JavaScript" type="text/javascript"> function showTimeLimit<%=this.ClientID %>(str, end, now) { var now = new Date(now); var future = new Date(str); // var now = document.getElementById('Hidden1').getAttribute("nowtime"); future += 1000; if (now - future < 0) { var days = (future - now) / 1000 / 60 / 60 / 24; var dayNum = Math.floor(days); var hours = (future - now) / 1000 / 60 / 60 - (24 * dayNum); var houNum = Math.floor(hours); if (houNum < 10) { houNum = "0" + houNum; } var minutes = (future - now) / 1000 / 60 - (24 * 60 * dayNum) - (60 * houNum); var minNum = Math.floor(minutes); if (minNum < 10) { minNum = "0" + minNum; } var seconds = (future - now) / 1000 - (24 * 60 * 60 * dayNum) - (60 * 60 * houNum) - (60 * minNum); var secNum = Math.floor(seconds); if (secNum < 10) { secNum = "0" + secNum; } document.getElementById('TimeLimit1<%=this.ClientID %>').innerHTML = "<span class=\"style1\">距秒杀开始还有:</span><br/><span class=\"style2\">" + dayNum + "</span>天<span class=\"style2\">" + houNum + "</span>小时<span class=\"style2\">" + minNum + "</span>分<span class=\"style2\">" + secNum + "</span>秒"; } else { // setInterval('showTimeLimit(\"2012/09/29 09:11:50\",\"0\")', 1000); var future = new Date(end); // var future = new Date(); // future -= 1000; if (now - future < 0) { var days = (future - now) / 1000 / 60 / 60 / 24; var dayNum = Math.floor(days); var hours = (future - now) / 1000 / 60 / 60 - (24 * dayNum); var houNum = Math.floor(hours); if (houNum < 10) { houNum = "0" + houNum; } var minutes = (future - now) / 1000 / 60 - (24 * 60 * dayNum) - (60 * houNum); var minNum = Math.floor(minutes); if (minNum < 10) { minNum = "0" + minNum; } var seconds = (future - now) / 1000 - (24 * 60 * 60 * dayNum) - (60 * 60 * houNum) - (60 * minNum); var secNum = Math.floor(seconds); if (secNum < 10) { secNum = "0" + secNum; } document.getElementById('TimeLimit1<%=this.ClientID %>').innerHTML = "<span class=\"style1\">距秒杀结束还有:</span><br/><span class=\"style2\">" + dayNum + "</span>天<span class=\"style2\">" + houNum + "</span>小时<span class=\"style2\">" + minNum + "</span>分<span class=\"style2\">" + secNum + "</span>秒"; } else { document.getElementById('TimeLimit1<%=this.ClientID %>').innerHTML = "<span class=\"style1\">秒杀已结束</span>"; } } } </script> <span id="TimeLimit1<%=this.ClientID %>">请稍等,正在载入中。。。</span> <asp:Literal runat="server" ID="lblTime1"></asp:Literal> 后台: 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 Seckill : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { this.lblTime1.Text = "<script language=\"javascript\" type=\"text/javascript\">setInterval('showTimeLimit" + this.ClientID + "(\"" + StartSeckill + "\",\"" + EndSeckill + "\",\"" + System.DateTime.Now + "\")', 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; } } } }
jisuanjizhuan 2012-10-02
  • 打赏
  • 举报
回复
学习了!万分感谢!
孟子E章 2012-10-02
  • 打赏
  • 举报
回复
最小的改动的实现方法


<script language="JavaScript" type="text/javascript">

function showTimeLimit<%=this.ClientID %>(str, end) {
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.getElementById('TimeLimit1<%=this.ClientID %>').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.getElementById('TimeLimit1<%=this.ClientID %>').innerHTML = "距秒杀结束还有:<span class=\"style1\">" + dayNum + "</span>天<span class=\"style1\">" + houNum + "</span>小时<span class=\"style1\">" + minNum + "</span>分<span class=\"style1\">" + secNum + "</span>秒";
}
else {

document.getElementById('TimeLimit1<%=this.ClientID %>').innerHTML = "<span clas=\"style1\">秒杀已结束</span>";

}
}
}

</script>
<span id="TimeLimit1<%=this.ClientID %>">请稍等,正在载入中。。。</span>
<asp:Literal runat="server" ID="lblTime1"></asp:Literal>



 protected void Page_Load(object sender, EventArgs e)
{
this.lblTime1.Text = "<script language=\"javascript\" type=\"text/javascript\">setInterval('showTimeLimit" + this.ClientID + "(\"" + StartSeckill + "\",\"" + EndSeckill + "\")', 1000);</script>";
}
jisuanjizhuan 2012-10-02
  • 打赏
  • 举报
回复
急,各位大侠帮帮忙,怎么加上控件的ClientID,初学者在此讨教。
孟子E章 2012-10-01
  • 打赏
  • 举报
回复
你这些js的函数名、变量都是相同的,会相互覆盖的,所以说不行的,明白道理了吗。你应该采用闭包或者加上控件的ClientID才可以

另外, document.getElementsByName('TimeLimit1')

<span id="TimeLimit1" name="TimeLimit1">

对非表单控件是无效的
jisuanjizhuan 2012-10-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
WebForm1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication7.WebForm1" %>
<%@ Register src="re_Seckill.ascx" tagname="re_Seckill" tagprefix=……
[/Quote]
试了,说的没错,但怎么改我还是不太明白,指教!

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧