views:

407

answers:

2

I want to display time in textbox or in something like a numericupdownextender used in AJAX so that the user can change time as he desires..

i have used the control to show numbers and increase accordingly..

is there a way to do this..

new code but not what is desired...

<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>

the code behind is:

 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;
    }
A: 

hi,

Hrmm, I recommend you spend some time playing around with jQuery if you haven't. Hope it helps.

jQuery UI

John Resig

lovemore-world has a good article that hopefuly can inspire you and get the creative juices going.

elsharpo
The usual SO response of "you should use jQuery", without any attempt to answer the question. The AJAX toolkit already provides an out of the box control that can handle this problem. Thanks for the down vote btw.
fearofawhackplanet
I didn't down vote you.
elsharpo
lol looks like everyone is getting down voted on this question! maybe the down voters could try and help with a solution instead of just down voting those who are trying to help.
fearofawhackplanet
agree..I know my answer was standard "jQuery-look-into-it" but it doesn't hurt having a look at it or at least being aware of it. :-) I'll be more constructive next time though.
elsharpo
+2  A: 

Yes this should be pretty simple to achieve using the updownextender. Just attach web service methods to the serviceupmethod and servicedownmethod which increment/decrement your datetime by the required timespan. you haven't posted any code so it's difficult to know where you are stuck.

UPDATE: ok, so having thought about this, I don't think there is any real reason to use an updownextender with server call backs. A quick google discovered that javascript already has some basic date manipulation functions, so it's easy enough to do everything client side.

I'm not a javascript expert, so the following code is possibly of questionable quality, but it seems to work ok and hopefully will get you set on the right track. Let me know if you still get stuck.

<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>
fearofawhackplanet
no i cant find any example which does this so im stuck... i got the date but nothing for time...
i have updated some part... any help will be appreciated... thanks
some feedback as to why i'm being downvoted would be appreciated, so in future i can try and provide better answers.
fearofawhackplanet
@unknown, i'm going to bed now (it's 2 in the morning here). if you are still stuck by tomorrow evening i will try and put some example code together for you.
fearofawhackplanet
I don't see any reason why that perfectly sensible answer was downvoted...
Thomas Levesque
thanks man... i need the help... i searched and searched and there is nothing... i have come up with something which i have put up... but thats not what i want... cant believe microsoft did not have this...