【问题标题】:display time in a textbox and modify with up down arrows在文本框中显示时间并使用向上向下箭头进行修改
【发布时间】:2010-01-11 23:17:07
【问题描述】:

我想在文本框或 AJAX 中使用的 numericupdownextender 之类的东西中显示时间,以便用户可以根据需要更改时间..

我已使用控件显示数字并相应增加..

有没有办法做到这一点..

新代码,但不是我们想要的...

<asp:TextBox runat="server" ID="txtHour"></asp:TextBox>
<ajaxToolkit:NumericUpDownExtender ID="txtHour_NumericUpDownExtender" runat="server"   Enabled="True" Maximum="12" Minimum="1" TargetControlID="txtHour" Width="70"></ajaxToolkit:NumericUpDownExtender>

<asp:TextBox runat="server" ID="txtMinute"></asp:TextBox>
 <ajaxToolkit:NumericUpDownExtender ID="txtMinute_NumericUpDownExtender" runat="server" Enabled="True" Maximum="60" Minimum="1"  TargetControlID="txtMinute" Width="70"></ajaxToolkit:NumericUpDownExtender>

 <asp:TextBox runat="server" ID="txtDayPart"></asp:TextBox>
 <ajaxToolkit:NumericUpDownExtender ID="txtDayPart_NumericUpDownExtender" runat="server" Enabled="True" RefValues="AM;PM" TargetControlID="txtDayPart" Width="70"></ajaxToolkit:NumericUpDownExtender>

后面的代码是:

 private void ParseTime(string TimeString)
    {

        // Validation of input

        if (TimeString.IndexOf(":") == -1)
        {

            return;
        }

        if ((TimeString.IndexOf("PM") == -1) && (TimeString.IndexOf("AM") == -1))
        {

            return;
        }

        // Good to go with format

        int ColonPos = TimeString.IndexOf(":");
        int AMPos = TimeString.IndexOf("AM");

        int PMPos = TimeString.IndexOf("PM");
        string sHour = TimeString.Substring(0, ColonPos);

        string sMinutes = TimeString.Substring(ColonPos, 3); string sDayPart = (TimeString.IndexOf("AM") != -1) ? TimeString.Substring(AMPos, 2) : TimeString.Substring(PMPos, 2);
        txtHour.Text = sHour;

        txtMinute.Text = sMinutes;

        txtDayPart.Text = sDayPart;
    }

【问题讨论】:

  • 我已经编辑了我的帖子以展示一个可能的解决方案。我不确定编辑是否会提醒您新的答案(我希望不会),所以希望这条评论会被标记出来。

标签: c# asp.net ajax time


【解决方案1】:

是的,这应该很容易使用 updownextender 来实现。只需将 Web 服务方法附加到 serviceupmethod 和 servicedownmethod,它们会按所需的时间跨度增加/减少您的日期时间。你还没有发布任何代码,所以很难知道你卡在哪里了。

更新:好的,考虑到这一点,我认为没有任何真正的理由使用带有服务器回调的 updownextender。一个快速的 google 发现 javascript 已经有一些基本的日期操作功能,所以它很容易做所有客户端的事情。

我不是 javascript 专家,因此以下代码的质量可能有问题,但它似乎工作正常,希望能让您走上正轨。如果您仍然卡住,请告诉我。

<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        <!--
            var date;

            function initDateObject()
            {
                date = new Date ( "January 1, 2000 12:00:00" );
                showTimePortion();
            }

            function showTimePortion()
            {
                document.getElementById('timeDisplay').value = padToMinimumLength(date.getHours(),2) + ':' + padToMinimumLength(date.getMinutes(),2) + ':' + padToMinimumLength(date.getSeconds(),2);
            }

            function padToMinimumLength(number, requiredLength)
            {
                var pads = requiredLength - (number + '').length;
                while (pads > 0)
                {
                    number = '0' + number;
                    pads--;
                }
                return number;
            }

            function addMinutes(n)
            {
                date.setMinutes(date.getMinutes() + n);
                showTimePortion();
            }

            function setTodaysTime()
            {
                var d = new Date();
                d.setHours(date.getHours());
                d.setMinutes(date.getMinutes());
                d.setSeconds(date.getSeconds());
                alert('the time is now ' + d.toString());
            }
        -->
    </script>
</head>
<body onload="initDateObject();">
    <form id="form1" runat="server">
    <div>
        <input type="text" id="timeDisplay" readonly="readonly"/>
        <a href="#" onclick="addMinutes(1)">+</a>
        <a href="#" onclick="addMinutes(-1)">-</a>
        <br />
        <a href="#" onclick="setTodaysTime()">Submit</a>
    </div>
    </form>
</body>

【讨论】:

  • 不,我找不到任何这样做的例子,所以我卡住了......我得到了日期,但没有时间......
  • 我已经更新了一些部分...任何帮助将不胜感激...谢谢
  • 一些关于我为什么被否决的反馈将不胜感激,所以将来我可以尝试提供更好的答案。
  • @unknown,我现在要睡觉了(这里是凌晨 2 点)。如果明天晚上你仍然卡住,我会尝试为你整理一些示例代码。
  • 谢谢哥们...我需要帮助...我搜索了又搜索了,但什么也没有...我想出了一些我已经提出的东西...但那不是我想要的想要...不敢相信微软没有这个...
【解决方案2】:

嗯,如果你还没有,我建议你花一些时间玩弄 jQuery。希望对您有所帮助。

jQuery UI

John Resig

lovemore-world 有一篇好文章,希望能激发您的灵感并激发创意。

【讨论】:

  • “你应该使用 jQuery”的常见 SO 响应,没有任何尝试回答问题。 AJAX 工具包已经提供了一个开箱即用的控件来处理这个问题。感谢您的反对票。
  • 大声笑看起来每个人都对这个问题投了反对票!也许反对的选民可以尝试帮助解决方案,而不是仅仅反对那些试图提供帮助的人。
  • 同意..我知道我的回答是标准的“jQuery-look-into-it”,但看看它或至少意识到它并没有什么坏处。 :-) 不过我下次会更有建设性。
猜你喜欢
  • 1970-01-01
  • 2011-04-30
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-17
相关资源
最近更新 更多