Using DateTime Picker With Null Support with MS Access and ASP.Net
page 1 of 2
Published: 16 Apr 2004
Unedited - Community Contributed
Abstract
How to use date/time picker to send date/time value to database and how to send NULL value to date/time field in database through ASP.Net using VB.
by Sameer Lal
Feedback
Average Rating: 
Views (Total / Last 10 Days): 24463/ 34

Adding Date/Time picker to your web form:

Adding Date/Time picker to your web form:

 

Using date picker for input to Date/Time field always made my applications more user friendly and never bothered me and the users about formatting the date/time. It is very simple to add date picker into any of your web forms or search pages in ASP. Net. The built in calendar control comes out very handy.

Drag and drop a Text box (TextBox1) into your form and you can add either the image of calendar or add text with hyperlink to date picker page next to it.

 

  DatePicker

 

Now go to html view and change the codes as follows:

    <asp:textbox id="TextBox1" style="Z-INDEX: 105; LEFT: 384px; POSITION: absolute; TOP: 56px" runat="server" Width="80px"></asp:textbox><STRONG>&nbsp;</STRONG><STRONG>&nbsp; <A href="javascript:calendar_window=window.open('calendar1.aspx?formname=Form1.TextBox1','calendar_window','width=154,height=188');calendar_window.focus()"><STRONG><IMG style="Z-INDEX: 106; LEFT: 472px; POSITION: absolute; TOP: 56px" src="button_date.gif"></STRONG></A>

 

Now, create another webform, name it calendar1.aspx  and add the following codes on html page.

 

Add these codes in the head portion of html page:

            <script runat="server">

    Private Sub Calendar1_SelectionChanged(sender As Object, e As System.EventArgs)

        Dim strjscript as string = "<script language=""javascript"">"

        strjscript = strjscript & "window.opener." & Httpcontext.Current.Request.Querystring("formname") & ".value = '" & Calendar1.SelectedDate & "';window.close();"

        strjscript = strjscript & "</script" & ">" 'Don't Ask, Tool Bug

        Literal1.text = strjscript

    End Sub

   

    Private Sub Calendar1_DayRender(sender As Object, e As System.Web.UI.WebControls.DayRenderEventArgs)

       If e.Day.Date = datetime.now().tostring("d") Then

       e.Cell.BackColor = System.Drawing.Color.LightGray

       End If

    End Sub

            </script>

       

 

 

And add the following codes in the body:

 

      <body leftmargin="0" topmargin="0">

            <form runat="server" ID="Form1">

                  <asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_dayrender"

                        DayNameFormat="FirstLetter" BackColor="#FFFFCC" FirstDayOfWeek="Monday" BorderColor="#FFCC66"

                        ForeColor="#663399" Height="188px" Width="154px" BorderWidth="1px" Font-Size="8pt" Font-Names="Verdana"

                        ShowGridLines="True">

                        <TodayDayStyle ForeColor="White" BackColor="#FFCC66"></TodayDayStyle>

                        <SelectorStyle BackColor="#FFCC66"></SelectorStyle>

                        <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC"></NextPrevStyle>

                        <DayHeaderStyle Height="1px" BackColor="#FFCC66"></DayHeaderStyle>

                        <SelectedDayStyle Font-Bold="True" BackColor="#CCCCFF"></SelectedDayStyle>

                        <TitleStyle Font-Size="9pt" Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></TitleStyle>

                        <OtherMonthDayStyle ForeColor="#CC9966"></OtherMonthDayStyle>

                  </asp:Calendar>

                  <asp:Literal id="Literal1" runat="server"></asp:Literal>

            </form>

      </body>

 

And that is it. You are ready to use your date picker to enter the date value through text box into your date/time field of database.


View Entire Article

User Comments

Title: Fantstic   
Name: Jackson Desai
Date: 2009-12-01 5:51:54 AM
Comment:
Its a jenuin solution that I want for my Web Appliction .Thanx.
Title: No value   
Name: Raves
Date: 2008-12-23 3:55:53 AM
Comment:
No value coming after clicking the date and windows is not closing ...
Title: Perfect Matter   
Name: Ajay
Date: 2008-09-11 9:00:32 AM
Comment:
Is't a very use ful matter for me
Title: requst   
Name: vinod
Date: 2008-06-12 6:05:42 AM
Comment:
but the popup window is not closing after selecting a value... can u explain how to do that also...
Title: null to date format   
Name: Kris Tali
Date: 2007-02-21 1:27:42 AM
Comment:
the problem is that you can't put "" and dbnull to date
try my solution
Dim IDateTo
If Not IsDate(String.Format("{0:MMM-dd-yyyy}", Me.TxtDateTo.Text)) Then
IDateTo = DBNull.Value
Else
IDateTo = CDate(String.Format("{0:MMM-dd-yyyy}", Me.TxtDateTo.Text))
End If
Title: Date   
Name: Ak
Date: 2006-12-28 1:08:30 PM
Comment:
Execellent. I was struggling to make this work.
Title: Perfect Match   
Name: Maulik Dave
Date: 2006-12-03 9:54:49 AM
Comment:
Perfect Match! Just what I was looking for. Even though an article is very old (2004), I was able to use it without trouble in .net 2.0
Title: Asp.net 2.0 instructions   
Name: Art
Date: 2006-11-01 5:42:36 PM
Comment:
I tried this, and it would not work for asp.net 2.0

I finally figured it out. asp.net renames your controls when it renders the page. It also renames your form. So, when you send the literal Form1.TextBox1, that is not what asp.net 2.0 renders in the web browser. In reality, its 'aspnetForm' for Form1 (the form name) and '_ctl0_Main_TextBox1' for TextBox1.

When I altered the javascript in calendar1.SelectionChanged, it worked.
Title: Mr   
Name: Babu ( Eranavoor ),Rajesh,Arun,Pondy(Gopal),Rms,Vijay
Date: 2006-10-25 6:15:44 AM
Comment:
Excellent
Title: Calendar failure in master Pages.   
Name: Laserman
Date: 2006-09-29 4:42:47 PM
Comment:
Hi. We are using C# and had success until we decide to
use a Master Page for all our web pages.

The calendar will come up but then, after clicking the date,
the form1.txtFieldname is said to be null or not an object.
We tried adding the id of the content placeholder
asp:contentplaceholder
which did not work either. Can you help? Thanks.
Title: Code compatibility   
Name: Vineet Bhandari
Date: 2006-09-01 6:26:50 AM
Comment:
The code doesn't work with asp.net 2.0 . Please give some 2.0 compatible code. Neither it displays the code in the text box nor the window gets closed
Title: nice like mandya sugar   
Name: mandyavenkatesh
Date: 2006-07-27 6:28:01 AM
Comment:
excellent topic we will except some more asp.net issues like this
Title: Miss   
Name: Satya Krishna
Date: 2006-07-26 1:50:29 AM
Comment:
Thanks for the help. It reached my requirement.
Title: Very Nice   
Name: arijit chatterjee
Date: 2006-07-21 3:45:12 AM
Comment:
Very Nice Code.
But how to fix the always on top of the calling page ?
Someone asked for same for windows application. he is a fool. It is already there.
Title: workable as standalone programme?   
Name: kelvin
Date: 2006-06-26 10:50:48 PM
Comment:
can i use this on a windows application instead of a web application. is it still workable?
Title: Ms   
Name: Dhivya
Date: 2006-06-16 5:21:24 AM
Comment:
Very nice,and working gud
Title: In a FormView   
Name: Bryan
Date: 2006-06-13 11:51:35 AM
Comment:
Anyone try to make this work inside of a FormView control? I'm having a problem where it fails to close the calendar as well as failing to transfer the selected date back to the textbox. It's all inside of a FormView in the EditItemTemplate.
Title: Excellent Job   
Name: Jignesh Patel
Date: 2006-06-09 12:46:26 PM
Comment:
Excellent..This is what I really wanted. Thanks a lot.
Title: Cool   
Name: dhruvin
Date: 2006-05-23 2:25:02 PM
Comment:
keep it up very good.i have no words to expalin
Title: figured out!!   
Name: kid
Date: 2006-05-16 9:42:59 PM
Comment:
kid found the solution to the problem...
it is to ignore and run the code.. it works!!
thnx
Title: help... error!!   
Name: kid
Date: 2006-05-16 1:53:17 AM
Comment:
i was prompt after writing the script which needed to be place in the head portion of calendar1.aspx

error falls in this area:
strjscript = strjscript & "</script" & ">"
'Don't Ask, Tool Bug...

wat should i do??
Title: Code for ASP.net 2.0??   
Name: vLo
Date: 2006-03-29 3:51:11 PM
Comment:
Has anyone make this worked with ASP.net 2.0?? I used it w/ ASP.net 1.1 and had no problem. I am not sure why it doesn't close the window, nor bring in the date to the textbox :(

Please provide any tip you have. Thanks!
Title: Thanks Sameer ji   
Name: Omprakash
Date: 2006-01-14 12:57:25 AM
Comment:
Thanks a lot , this is now easy for me to go
my email
omprakash.dashore@gmail.com
Title: Multiple Date time pickers   
Name: c
Date: 2005-10-25 8:46:48 AM
Comment:
Has anyone tried to use more than one on a page? I have 3 textboxes 3 images etc.. but only one will return the date to the textbox. I have the correct textbox ids in the code and it seems to be adding the right javascript to the literal control?
Title: sql server   
Name: balaji
Date: 2005-09-27 4:26:03 AM
Comment:
how it change the code for sql server?
where to put this? in code behind or class function
Title: date wont work   
Name: davetree
Date: 2005-09-16 5:44:45 AM
Comment:
make sure the textboxes that you have are of the same id as the line 'calendar1.aspx?formname=Form1.Textbox1{change this textbox name to the name of your textbox(s) ie i have 1 as txtfromDate so the line is 'calendar1.aspx?formname=Form1.txtfromDate}
That was how i solved my problem which was the same as you said urs was - hope it helps you
Title: it's not working for me   
Name: Kim
Date: 2005-09-14 11:07:32 AM
Comment:
The text doesn't display in the textbox
and the calender window doesn't close !!
Please help
Title: For it to work in Firefox... Take 2   
Name: Eric Brousseau
Date: 2005-08-09 5:37:35 PM
Comment:
Sorry, I made a typo in the script in my last post. There is a dot after the word "document"

strjscript = strjscript & "window.opener.document." & Httpcontext.Current.Request.Querystring("formname") & ".value = '" & Calendar1.SelectedDate & "';window.close();"

By the way, your idea was realy helpfull. Keep up the good work!
Title: For it to work in Firefox...   
Name: Eric Brousseau
Date: 2005-08-09 5:26:08 PM
Comment:
This code works well in Internet Explorer, but not in Firefox. If you want it to work, you must modify the JavaScript part from the calendar page like this:

strjscript = strjscript & "window.opener.document" & Httpcontext.Current.Request.Querystring("formname") & ".value = '" & Calendar1.SelectedDate & "';window.close();"

See, the "document" word is missing from your code and it crashes the FirefoxJavaScript compiler... Seems like IE is able to "understand" it both way.

And also, you should use RegisterStartupScript to add the JavaScript code to the WebForm in place of a Literal.
Title: Great!   
Name: Pancho
Date: 2005-08-03 2:06:19 PM
Comment:
It works great, thankyou sooooo much, thats what i was looking for :D
Title: not working for me   
Name: kkk
Date: 2005-06-28 4:38:03 AM
Comment:
I am not able to dispaly selected date value in textbox
Title: Mr   
Name: hari
Date: 2005-06-13 3:12:32 AM
Comment:
thats great.
thnx a lot for help.
Title: Beautiful   
Name: Developer
Date: 2005-04-21 12:08:02 PM
Comment:
Works like a charm. Exactly what my project needed. Do you plan to put up a version with date and time??

Thanks
Title: Sweet   
Name: Mark
Date: 2005-04-06 6:02:07 AM
Comment:
Excellent. That was exactly what I was looking for, nothing more, nothing less.
Title: Problem in ur Pop Up Calendar   
Name: Baber Saeed
Date: 2005-01-09 10:42:27 AM
Comment:
When we seconds Time try to select another dayte..It does nothing......unless we don't refresh the page it doesn't work..Any solution to this problem.
Title: Wonderful   
Name: Baber Saeed
Date: 2005-01-09 8:46:17 AM
Comment:
Excellent..This is what I really wanted. Thanks a lot.
Title: Mr.   
Name: David Teasdale
Date: 2004-11-19 7:31:43 AM
Comment:
Excellent..Brilliant...Thanks for the help.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 10:50:29 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search