-2

リストビューアイテムにタイマーを実装しようとしています。私のJavaScriptは次のとおりです。

function countdown() {
var now = new Date();
var biddate = document.getElementById('InitiateDate').value
var kickoff = Date.parse(biddate);
var diff = kickoff - now;

var days = Math.floor(diff / (1000 * 60 * 60 * 24));
var hours = Math.floor(diff / (1000 * 60 * 60));
var mins = Math.floor(diff / (1000 * 60));
var secs = Math.floor(diff / 1000);

var dd = days;
var hh = hours - days * 24;
var mm = mins - hours * 60;
var ss = secs - mins * 60;

if (mm<10) {
    mm = "0" + mm;
}
if (ss<10) {
    ss = "0" + ss;
}


document.getElementById("CountDownTimer")
            .innerHTML =
                dd + ':' +
                hh + ':' +
                mm + ':' +
                ss;

if (dd < 0) {
    document.getElementById("CountDownTimer")
            .innerHTML = "time up";
}

}
setInterval('countdown()', 1000);

私のリストビューアイテムテンプレートの関連部分は次のとおりです。

    <td>
    <asp:TextBox ID="InitiateDate" runat="server" Text='<%#  Eval("InitiateDate") %>'  ReadOnly="True"  ClientIDMode="Static"></asp:TextBox>
    <div id="CountDownTimer" class="timer"> </div>
    </td>

このjavascriptは正常に機能していますが、listview.iの最初の行に対してのみ機能する必要があり、すべての行に対してカウントダウンタイマーを提供します。

4

6 に答える 6

0
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            TextBox InitiateDate = e.Item.FindControl("InitiateDate") as TextBox;
            HtmlGenericControl CountDownTimer = e.Item.FindControl("CountDownTimer") as HtmlGenericControl;

            ScriptManager.RegisterStartupScript(InitiateDate, this.GetType(), "myscript", "countdown('"+ InitiateDate.ClientID +"', '"+ CountDownTimer.ClientID +"');", true);
        }
    }

スクリプトとして\

    function countdown(InitiateDate,CountDownTimer) {
..
..
var biddate = document.getElementById(InitiateDate).value
..
..

document.getElementById(CountDownTimer).innerHTML=dd + ':' +
            hh + ':' +
            mm + ':' +
            ss;
}
于 2013-03-18T08:51:22.987 に答える
0
   protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem )
        {
            Label InitiateDate = e.Item.FindControl("InitiateDate") as Label;
            HtmlGenericControl CountDownTimer = e.Item.FindControl("CountDownTimer") as HtmlGenericControl;

           ScriptManager.RegisterStartupScript(InitiateDate, this.GetType(), "AKey" + e.Item.DataItemIndex, "countdown(" + InitiateDate.ClientID + " ," + CountDownTimer.ClientID + ");", true);
}
    }

脚本

var biddate, InitiateDateTemp, CountDownTimerTemp;
    function countdown(InitiateDate, CountDownTimer) {
        InitiateDateTemp = InitiateDate;
        CountDownTimerTemp = CountDownTimer;




      biddate = InitiateDate.innerHTML;

 //   setInterval(function () { SetTime(biddate) }, 1000);

  //  var stopTimer;
    var timerId = setInterval(function () {

       var dd= SetTime(biddate);

       if (dd <= 0) {
            clearInterval(timerId);
        }
    }, 1000);

}

function  SetTime(biddate)  {
    var kickoff = Date.parse(biddate);
    var now = Date.now();
    var diff = kickoff - now;
   // debugger;
    var days = Math.floor(diff / (1000 * 60 * 60 * 24));
    var hours = Math.floor(diff / (1000 * 60 * 60));
    var mins = Math.floor(diff / (1000 * 60));
    var secs = Math.floor(diff / 1000);

    var dd = days;
    var hh = hours - days * 24;
    var mm = mins - hours * 60;
    var ss = secs - mins * 60;

    if (mm < 10) {
        mm = "0" + mm;
    }
    if (ss < 10) {
        ss = "0" + ss;
    }


    CountDownTimerTemp.innerHTML =
                     dd + ':' +
                     hh + ':' +
                     mm + ':' +
                     ss;



    if (dd < 0) {
        CountDownTimerTemp
                 .innerHTML = "time up";
        return -1;

    }
    else {
        return 1;
    }
}

データリストのラベルとdiv

  <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
            </asp:DataList>
        </p>
        <p>
            <asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound">
                <ItemTemplate >


                    <asp:Label ID="InitiateDate" Text='<%# Eval ("datetime") %>' runat="server" ></asp:Label>

                    <div id="CountDownTimer" runat="server" >Timer</div>
                </ItemTemplate>
            </asp:ListView>
于 2013-03-19T06:53:58.187 に答える
0
 var biddate, InitiateDateTemp, CountDownTimerTemp;

        function countdown(InitiateDate, CountDownTimer) {
            InitiateDateTemp = InitiateDate;
            CountDownTimerTemp = CountDownTimer;

            biddate = InitiateDate.innerHTML;


               var dd= SetTime(biddate);


        }




        function  SetTime(biddate)  {
            var kickoff = Date.parse(biddate);
            var now = Date.now();
            var diff = kickoff - now;
           // debugger;
            var days = Math.floor(diff / (1000 * 60 * 60 * 24));
            var hours = Math.floor(diff / (1000 * 60 * 60));
            var mins = Math.floor(diff / (1000 * 60));
            var secs = Math.floor(diff / 1000);

            var dd = days;
            var hh = hours - days * 24;
            var mm = mins - hours * 60;
            var ss = secs - mins * 60;

            if (mm < 10) {
                mm = "0" + mm;
            }
            if (ss < 10) {
                ss = "0" + ss;
            }


            CountDownTimerTemp.innerHTML =
                             dd + ':' +
                             hh + ':' +
                             mm + ':' +
                             ss;



            if (dd < 0) {
                CountDownTimerTemp
                         .innerHTML = "time up";
                return -1;

            }
            else {
                return 1;
            }
        }




  protected void Timer1_Tick(object sender, EventArgs e)
    {
        Timer time = (Timer)sender;
        ListViewItem item = (ListViewItem)time.Parent;
        ListView lst = (ListView)time.Parent.Parent ;

        foreach (ListViewItem listItem in lst.Items )
        {

            Label InitiateDate = item.FindControl("InitiateDate") as Label;
            HtmlGenericControl CountDownTimer = item.FindControl("CountDownTimer") as HtmlGenericControl;
            ScriptManager.RegisterStartupScript(InitiateDate, this.GetType(), "AKey" + item.DataItemIndex, "countdown(" + InitiateDate.ClientID + " ," + CountDownTimer.ClientID + ");", true);

        }

    }

ページ読み込みイベントで、このコードを追加します。これは私のテストコードです

//very important code.    (if (IsPostBack == false))
  if (IsPostBack == false)
        {
            List<A> data = new List<A>();
            A a = new A();
            A a1 = new A();
            a.Leave_Status = "A";
            a.datetime = DateTime.Now.AddDays(1);
            data.Add(a);
            a1.Leave_Status = "A1";
            a1.datetime = DateTime.Now.AddDays(-1);

            data.Add(a1);
            ListView1.DataSource = data;
            ListView1.DataBind();
        }
于 2013-03-20T06:39:14.533 に答える
0
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script>


        var biddate, InitiateDateTemp, CountDownTimerTemp;

        function countdown(InitiateDate, CountDownTimer) {
            InitiateDateTemp = InitiateDate;
            CountDownTimerTemp = CountDownTimer;

            biddate = InitiateDate.innerHTML;

            var kickoff = Date.parse(biddate);
            var now = Date.now();
            var diff = kickoff - now;
            //debugger;
            var days = Math.floor(diff / (1000 * 60 * 60 * 24));
            var hours = Math.floor(diff / (1000 * 60 * 60));
            var mins = Math.floor(diff / (1000 * 60));
            var secs = Math.floor(diff / 1000);

            var dd = days;
            var hh = hours - days * 24;
            var mm = mins - hours * 60;
            var ss = secs - mins * 60;

            if (mm < 10) {
                mm = "0" + mm;
            }
            if (ss < 10) {
                ss = "0" + ss;
            }

             if (dd < 0) {
                CountDownTimerTemp
                         .innerHTML = "time up";
            }
            else {
                CountDownTimerTemp.innerHTML =
                            dd + ':' +
                            hh + ':' +
                            mm + ':' +
                            ss;
            }
        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server"  >
        </asp:ScriptManager>



        <p>
            &nbsp;</p>
        <p>

        </p>
        <p>
            <asp:ListView   ID="ListView1" runat="server"   >
                <ItemTemplate >
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                        <ContentTemplate >
                     <asp:Label   ID="InitiateDate" Text='<%# Eval ("datetime") %>' runat="server" ></asp:Label>

                   <div id="CountDownTimer" runat="server" >Timer</div>
                        </ContentTemplate>
                        <Triggers> 
                            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                        </Triggers>
                    </asp:UpdatePanel>
                    <asp:Timer ID="Timer1"  runat="server" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>

                </ItemTemplate>
            </asp:ListView>
        </p>
        <p>
            &nbsp;</p>
    </form>
</body>
</html>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            List<A> data = new List<A>();
            A a = new A();
            A a1 = new A();
            a.Leave_Status = "A";
            a.datetime = DateTime.Now.AddDays(1);
            data.Add(a);
            a1.Leave_Status = "A1";
            a1.datetime = DateTime.Now.AddDays(-1);

            data.Add(a1);
            ListView1.DataSource = data;
            ListView1.DataBind();
        }

    }

    class A
    {
        public string Leave_Status { get; set; }
        public DateTime  datetime { get; set; }
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Timer time = (Timer)sender;
        ListViewItem item = (ListViewItem)time.Parent;
        ListView lst = (ListView)time.Parent.Parent ;

        foreach (ListViewItem listItem in lst.Items )
        {

            Label InitiateDate = item.FindControl("InitiateDate") as Label;
            HtmlGenericControl CountDownTimer = item.FindControl("CountDownTimer") as HtmlGenericControl;
            if (CountDownTimer.InnerHtml != "time up")
            {
                ScriptManager.RegisterStartupScript(InitiateDate, this.GetType(), "AKey" + item.DataItemIndex, "countdown(" + InitiateDate.ClientID + " ," + CountDownTimer.ClientID + ");", true);
            }
        }

    }
}
于 2013-03-20T09:22:09.917 に答える
0

脚本

    var biddate, InitiateDateTemp, CountDownTimerTemp;

    function countdown(InitiateDate, CountDownTimer) {
        InitiateDateTemp = InitiateDate;
        CountDownTimerTemp = CountDownTimer;

        biddate = InitiateDate.innerHTML;

        var kickoff = Date.parse(biddate);
        var now = Date.now();
        var diff = kickoff - now;
        //debugger;
        var days = Math.floor(diff / (1000 * 60 * 60 * 24));
        var hours = Math.floor(diff / (1000 * 60 * 60));
        var mins = Math.floor(diff / (1000 * 60));
        var secs = Math.floor(diff / 1000);

        var dd = days;
        var hh = hours - days * 24;
        var mm = mins - hours * 60;
        var ss = secs - mins * 60;

        if (mm < 10) {
            mm = "0" + mm;
        }
        if (ss < 10) {
            ss = "0" + ss;
        }

        if (dd < 0) {
            CountDownTimerTemp
                     .innerHTML = "time up";
        }
        else {
            CountDownTimerTemp.innerHTML =
                        dd + ':' +
                        hh + ':' +
                        mm + ':' +
                        ss;
        }
    }

</script>

ASP.net

<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
<div>
<asp:ListView ID="Listview1" runat="server">
    <ItemTemplate>
                <tr style="_background-color:#DCDCDC;color: #000000;">

                    <td>
                        <asp:Label ID="field1" runat="server" Text='<%# Eval("field1") %>' />
                    </td>
                    <td>
                        <asp:Label ID="field2" runat="server" 
                            Text='<%# Eval("field2") %>' />
                    </td>
                    <td>
                        <asp:Label ID="field3" runat="server" 
                            Text='<%# Eval("field3") %>' />
                    </td>
                    <td>
                        <asp:Label ID="field4" runat="server" 
                            Text='<%# Eval("field4") %>' />
                    </td>
                    <td>
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                    <ContentTemplate >
                 <asp:Label   ID="InitiateDate" Text='<%# Eval ("InitiateDate") %>' runat="server" ></asp:Label>

               <div id="CountDownTimer" runat="server" >Timer</div>
                    </ContentTemplate>
                    <Triggers> 
                        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                    </Triggers>
                </asp:UpdatePanel>
                <asp:Timer ID="Timer1"  runat="server" Interval="3000" OnTick="Timer1_Tick"></asp:Timer>

                    </td>
                </tr>
            </ItemTemplate>
                <LayoutTemplate>
                <table id="Table1" runat="server" class="ListViewLayoutTable">
                    <tr id="Tr1" runat="server">
                        <td id="Td1" runat="server">
                            <table ID="itemPlaceholderContainer" runat="server" border="1" class="ListViewLayoutTable"
                                style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                                <tr id="Tr2" runat="server" style="background-color:#DCDCDC;color: #000000;">
                                    <th id="Th7" runat="server">
                                        field1</th>
                                    <th id="Th1" runat="server">
                                        field2</th>
                                    <th id="Th2" runat="server">
                                        field3</th>
                                    <th id="Th3" runat="server">
                                        field4</th>
                                    <th id="Th4" runat="server">
                                        timer</th>

                                </tr>
                                <tr ID="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>

                </table>
            </LayoutTemplate>
    </asp:ListView>
</div>

</form>
</body>
</html>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

public partial class Shops : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

    if (IsPostBack == false)
    {
        DataTable dt = new DataTable();

        dbServices service = new dbServices("MyConnection");
        SqlCommand command = new SqlCommand("SELECT * FROM table", service.SqlCon);
        SqlDataReader drRecord = command.ExecuteReader();
        dt.Load(drRecord);

        ShopBidsTable.DataSource = dt;
        ShopBidsTable.DataBind();
    }

}


protected void Timer1_Tick(object sender, EventArgs e)
{
    Timer time = (Timer)sender;
    ListViewItem item = (ListViewItem)time.Parent.Parent;
    ListView lst = (ListView)time.Parent.Parent.Parent;

    foreach (ListViewItem listItem in lst.Items)
    {

        Label InitiateDate = item.FindControl("InitiateDate") as Label;
        HtmlGenericControl CountDownTimer = item.FindControl("CountDownTimer") as HtmlGenericControl;
        if (CountDownTimer.InnerHtml != "time up")
        {
            ScriptManager.RegisterStartupScript(InitiateDate, this.GetType(), "AKey" + item.DataItemIndex, "countdown(" + InitiateDate.ClientID + " ," + CountDownTimer.ClientID + ");", true);
        }
    }

}

class A
{
    public string Leave_Status { get; set; }
    public DateTime datetime { get; set; }
}
}

これがすべて私のコードです...

于 2013-03-20T11:57:37.417 に答える
-2

解決しました!ティック機能はほとんど変更されていません。

protected void Timer1_Tick(object sender, EventArgs e)
{
    Timer time = (Timer)sender;
    ListViewItem item = (ListViewItem)time.Parent; 

    foreach (ListViewItem listItem in Listview1.Items)
    {
        Label InitiateDate = item.FindControl("InitiateDate") as Label;
        HtmlGenericControl CountDownTimer = item.FindControl("CountDownTimer") as HtmlGenericControl;
        if (CountDownTimer.InnerHtml != "time up")
        {
            ScriptManager.RegisterStartupScript(InitiateDate, this.GetType(), "AKey" + item.DataItemIndex, "countdown(" + InitiateDate.ClientID + " ," + CountDownTimer.ClientID + ");", true);
        }
    }
}

どうもありがとう。他の問題でお会いしましょう:)

于 2013-03-20T14:17:57.437 に答える