如何为DataList中的每个项目缩短时间,我在datalist中有一个项目的代码,但是我需要根据从数据库中重新获得的减少时间的javascript代码显示在DataList中的每个项目:
1-items.aspx页面:
<script type="text/javascript">
var leave =<%=seconds %>;
CounterTimer();
var interv=setInterval(CounterTimer,1000);
function CounterTimer()
{
var day = Math.floor(leave / ( 60 * 60 * 24))
var hour = Math.floor(leave / 3600) - (day * 24)
var minute = Math.floor(leave / 60) - (day * 24 *60) - (hour * 60)
var second = Math.floor(leave) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60)
hour=hour<10 ? "0" + hour : hour;
minute=minute<10 ? "0" + minute : minute;
second=second<10 ? "0" + second : second;
var d=day+" Days";
var h=hour+" H";
var m=minute+" M";
var s=second+" S";
document.getElementById("lbl1").innerText=d;
document.getElementById("lbl2").innerText=h;
document.getElementById("lbl3").innerText=m;
document.getElementById("lbl4").innerText=s;
leave=leave-1;
</script>
<asp:DataList ID="DataList1" runat="server" DataKeyField="item_id"
RepeatColumns="3"
RepeatDirection="Horizontal" Width="100%" ondatabinding="DataList1_DataBinding"
>
<ItemStyle Font-Bold="False" CssClass="data_list_item" />
<ItemTemplate>
<div class="thumbnail">
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "item_details.aspx?id="+Eval("item_id") %>'>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Bid_Increment") %>'
class="label label-important dosis-regular"></asp:Label>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("image") %>' AlternateText='<%# Eval("item_id") %>' Width="220px" Height="175px" />
<div class="caption">
<h4><%# Eval("name") %></h4>
</asp:HyperLink>
<p>
<asp:Button ID="btn_biding" runat="server" Text="Bid" class="btn ad-to"
onclick="btn_biding_Click" /><span class="price pull-right"> <span>Start price : <%# Eval("intialprice") %> </span></span></p>
</div>
<div>
<asp:Label ID="lbl1" runat="server" Width="50px" Height="30px"></asp:Label>
<asp:Label ID="lbl2" runat="server" Width="50px" Height="30px"></asp:Label>
<asp:Label ID="lbl3" runat="server" Width="50px" Height="30px"></asp:Label>
<asp:Label ID="lbl4" runat="server" Width="50px" Height="30px"></asp:Label>
</div>
</ItemTemplate>
<SeparatorStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" Wrap="False" />
</asp:DataList>
2-items.cs代码背后:
public double seconds;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AuctionConn"].ToString());
SqlCommand cmd;
SqlDataReader rd;
protected void Page_Load(object sender, EventArgs e)
{
seconds = (GetEndTime() - GetStartTime()).TotalSeconds;
}
private DateTime GetStartTime()
{
return DateTime.Now;
}
private DateTime GetEndTime()
{
cmd = new SqlCommand("select endDate from items", con);
con.Open();
rd = cmd.ExecuteReader();
rd.Read();
return Convert.ToDateTime(rd["endDate"]);
}