tags:

views:

323

answers:

3

Using ASP.Net & C#

I want to use a datetimepicker in my web page, i cannot find the control, How to get a datetime picker control in the web page.

Can any provide a sample code.

Need C# code Help.

+1  A: 

I use the jQuery datepicker on my pages. You might take a look at http://jqueryui.com/demos/datepicker/.

The use is that you just define a normal textbox in your c#/asp.net pages and on the client-side all the datepicking functions are added.

On the webpage you would have at least this part of the code:

     </script>
        $(function() {
           $("#YourTextBoxId").datepicker();
        });
     </script>

You find a working demo here: http://jqueryui.com/demos/datepicker/default.html

Yves M.
I added your script code in my webpage, but if i click the text box, calendar is not displaying...., i have to add some script?
Gopal
He said he wanted an answer using C#/.NET, not jQuery.
Will Morgan
+1  A: 

If you don't mind of using jQuery you can just use a textbox and covert it into a Date and time picker with jQuery.

more explanation on how to use on the jQuery site: http://plugins.jquery.com/project/timepicker-addon

Just include 3 javascript references to the page

<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.custom.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-timepicker-addon-0.5.min.js"></script>

the use the plugin as shown in the code below

// date and time
$('input').datetimepicker({...});
// or just time
$('input').timepicker({...});

EDIT: (added urls to the javascript files)

the Javascript files can be downloaded from the urls below:

http://trentrichardson.com/examples/timepicker/js/jquery-1.4.2.min.js

http://trentrichardson.com/examples/timepicker/js/jquery-ui-1.8.custom.min.js

http://trentrichardson.com/examples/timepicker/js/jquery-ui-timepicker-addon-0.5.min.js

EXTRA EXAMPLE:

<div class="example-container">
  <p>Show time in AM/PM 12 hour format</p>
  <div>
     <input type="text" id="example2" value="" />
  </div>
  <script type="text/javascript">
    $('#example2').datetimepicker({ampm: true});
  </script>
</div>
Wouter Janssens - Xelos bvba
Where i can i find the java script file..?, How to plugin in the code.
Gopal
How to add this code "$('input').datetimepicker({...});"
Gopal
I edited my answer an hope you can start using the plugin
Wouter Janssens - Xelos bvba
He said he wanted an answer using C#/.NET, not jQuery.
Will Morgan
How to add the plugins, where i have to add this line "$('input').datetimepicker({...});"
Gopal
@Gopal: I've added an extra example that you can copy and start using it. Make sure that if you change the id of the input that you change the id in the jQuery selector also.@Will: I am just suggesting a good solution, if he does not want it it to him to say that.
Wouter Janssens - Xelos bvba
Thanks for your reply, I need only the date not time. How to change the format
Gopal
@Gopal: The problem is that this is a timepicker where you can also pick a date with but it is not possible to pick a date without a time. Then I would suggest the Datepicker as Yves M. provided in this post.
Wouter Janssens - Xelos bvba
+1  A: 

You could use the ASP .NET AJAX Calendar Extender along with the standard textbox control.

See examples here

Once you have installed the Ajax Tool Kit (download from here) then it is a case of adding three lines to your web page:

 <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </asp:ToolkitScriptManager>
 <asp:TextBox runat="server" id="txtCalendar"></asp:TextBox>
 <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtCalendar"></asp:CalendarExtender>
Barry
I downloaded from the link, but how to install the ajax control tool kit
Gopal
See this link for details on how to install http://www.asp.net/ajaxlibrary/act.ashx The AJAX controls should then be in your toolbox and ready for use.
Barry
Am using vs2005, ajax control tool kit is not supporting, when i load the dll file, it is showing error
Gopal
You need to download the previous version http://ajaxcontroltoolkit.codeplex.com/releases/view/11121
Barry
I downloaded, but i cannot able to find the dll file to upload. How to install the ajax tool kit for vs2005
Gopal
@Gopal - once you have downloaded the zip file you need to open the AjaxControlToolkit solution and build it. Then in the AjaxControlToolkit folder a bin folder will appear. In the bin folder will be the AjaxControlToolkit.dll. You can then reference this in Visual Studio Toolbox.
Barry