2014-06-20 4 views
1

나는 시험이 끝날 때까지 카운트 다운을 보여 주어야하는 온라인 시험 프로젝트에 참여하고 있습니다. 내 웹에 배포 할 때asp.net에서 Ajax 타이머 감소를 올바르게 만드는 방법은 무엇입니까?

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     bind(); 
     bind1(); 
     Result(); 
     Session["Result"] = lblshow.Text; 

    if (!SM1.IsInAsyncPostBack) 
    { 
     SqlConnection sqlc1 = new SqlConnection(strcon); 

     sqlc1.Open(); 

     String str = "select top 1 * from TestCreated order by Id desc"; 
     SqlCommand cmd1 = new SqlCommand(str, sqlc1); 
     //sqlc.Open(); 
     SqlDataReader dr = cmd1.ExecuteReader(); 
     if (dr.Read()) 
     { 
      Session["timeout1"] = dr["TestDuration"].ToString(); 

     } 
     Session["timeout"] DateTime.Now.AddMinutes(Convert.ToInt32(Session["timeout1"].ToString())).ToString(); 
    } 

    }  

} 

protected void timer1_tick(object sender, EventArgs e) 
{ 
    if (Session["timeout"] == null) 
    Session["time"] = DateTime.Now.AddSeconds(10); 
    if (0 > DateTime.Compare(DateTime.Now, DateTime.Parse(Session["timeout"].ToString()))) 
    { 
     lblTimer.Text = string.Format("Time Left: 00:{0}:{1}",      ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).TotalMinutes).ToString(), ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).Seconds).ToString()); 

    } 

이 코드는 로컬 서버에서 잘 작동하지만 :

영문 코드 :

<asp:ScriptManager ID= "SM1" runat="server"></asp:ScriptManager> 
<asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional"> 
<ContentTemplate> 
<asp:Label ID="lblTimer" style=" margin-top:35px; margin-left:825px;" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="X-Large" ForeColor="#6600CC"></asp:Label> 
<div style="margin-right:25px;"> 
<asp:AlwaysVisibleControlExtender ID="AlwaysVisibleControlExtender1" runat="server" TargetControlID="lblTimer"> 
</asp:AlwaysVisibleControlExtender> 
<asp:Timer ID="timer1" runat="server" 
Interval="1000" OnTick="timer1_tick"></asp:Timer>  
    </div> 
</ContentTemplate> 
<Triggers> 
<asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" /> 
</Triggers> 
</asp:UpdatePanel> 

코드 뒤에이다 다음 코드를 사용했다 서버 타이머가 제대로 카운트 다운되지 않습니다 4 초 및 5 초이 감소합니다. 내 타이머 간격을 변경하여 시도했지만 작동하지 않습니다. 이 타이머를 어떻게 카운트 다운에 맞게 만들 수 있습니까? code project

+0

가 보이는 .. –

답변

0
// Add the ScriptManager and Timer Control. 

<div> 
<asp:ScriptManager ID= "SM1" runat="server"></asp:ScriptManager> 
<asp:Timer ID="timer1" runat="server" 
Interval="1000" OnTick="timer1_tick"></asp:Timer> 
</div> 
// Add the update panel, 
//a label to show the time remaining and the AsyncPostBackTrigger. 
<div> 
<asp:UpdatePanel id="updPnl" 
runat="server" UpdateMode="Conditional"> 
<ContentTemplate> 
<asp:Label ID="lblTimer" runat="server"></asp:Label> 
</ContentTemplate> 
<Triggers> 
<asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" /> 
</Triggers> 
</asp:UpdatePanel> 
</div> 

이 @Skoar 당신을 위해 작동합니다 ..

자세한 내용은 : 그것은 잘 작동처럼

protected void timer1_tick(object sender, EventArgs e) 
    { 
     if (0 > DateTime.Compare(DateTime.Now, 
     DateTime.Parse(Session["timeout"].ToString()))) 
     { 
      lblTimer.Text = "Number of Minutes Left: " + 
      ((Int32)DateTime.Parse(Session["timeout"]. 
      ToString()).Subtract(DateTime.Now).TotalMinutes).ToString(); 
     } 
}   
0

영문 코드에

관련 문제