我想从数据库中获取时间到用于在线试卷的计时器控件ASP.NET。 [英] I want fetch the time from database onto a timer control ASP.NET for online exam paper.

查看:43
本文介绍了我想从数据库中获取时间到用于在线试卷的计时器控件ASP.NET。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want fetch the time from database onto a timer control asp.net for online exam paper.



它输出的数量为分钟还剩119.

我还没知道这个时间是怎么来的。

请帮助这个。

预付谢谢。



我尝试过:




it giving an output as Number of minutes left 119.
am not getting how this time was came.
plz help with this.
advance thanks.

What I have tried:

<asp:ScriptManager runat="server" ID="Scrptmangr"></asp:ScriptManager>
                    <asp:Timer ID="timer1" runat="server" Interval="100" OnTick="timer1_Tick"></asp:Timer>
                    <asp:UpdatePanel runat="server" ID="Updatepanel">


                        <ContentTemplate>
                            <asp:Label ID="lblTimer" runat="server"></asp:Label>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="timer1" EventName="tick" />
                        </Triggers>

                    </asp:UpdatePanel>










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();
           }
       }

推荐答案

首先查看代码,然后考虑一下你的代码是做。您在会话中存储了什么,以及何时存储它?

如果您将其存储为除DateTime值以外的任何值,那么这很愚蠢 - 如果您要存储为DateTime,为什么要用它来捣乱呢?当你从Session中获取它时,只需将它转换为DateTime:

Start by looking at your code, and thinking about what you are doing. What did you store in the Session to start with, and when do you store it?
If you are storing it as anything other than a DateTime value, then that's silly - and if you are storing as a DateTime, why muck about with it? Just cast it to a DateTime when you fetch it from the Session:
protected void timer1_Tick(object sender, EventArgs e)
    {
    DateTime now = DateTime.Now;
    DateTime end = (DateTime)Session["timeout"];
    if (now < end)
        {
        lblTimer.Text = string.Format("Number of Minutes Left: {0}", (end - now).TotalMinutes);
        }
    }

但是......在Session中存储它不太可行:会话只有在服务器到期时才可用,默认为20分钟,不推荐在每个Session消耗资源时扩展它(一些托管服务不会让你扩展Session,有些托管服务也会把它缩短到5分钟或更短)。我建议他们在登录考试系统时将其存储在正版数据库中,因为这至少不受时间限制。



猜测,你的考试时间限制是2小时,你在页面加载事件中设置它,这就是它总是显示为119的原因 - 所有回发和第一次加载都发生页面加载。但是无法访问你的代码,我们无法确定。

我不会从C#执行此操作 - 我会从Javascript运行显示,其中包含主检查值如果是作弊行为的数据库。

But ... storing this in the Session is unlikely to work: the Session is only available until it expires on the server, which defaults to 20 minutes, and it is not recommended to extend that as each Session consumes resources (some hosting services won't let you extend the Session, and some of those cut it to five minutes or less as well). I'd suggest storing it in a genuine database when they log in to the exam system instead, as that is at least not subject to time restrictions.

At a guess, your exam time limit is 2 hours, and you are setting this in the page load event which is why it always shows as 119 - the page load occurs on all postbacks as well as one the first load. But without access to your code, we can't be sure.
I wouldn't do this from C# anyway - I'd do a running display from Javascript, with a "master check" value in a DB in case of cheating.


这篇关于我想从数据库中获取时间到用于在线试卷的计时器控件ASP.NET。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆