ASP.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a ASP.NET Expert!

Join 306,785 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 1,586 people online right now. Registration is fast and FREE... Join Now!




date time picker

 

date time picker

jainiraj

22 May, 2009 - 03:26 AM
Post #1

New D.I.C Head
*

Joined: 13 Nov, 2008
Posts: 15

I want to use calender like date time picker in asp.net+c#. how can I add this calender or date time picker.

User is offlineProfile CardPM
+Quote Post


eclipsed4utoo

RE: Date Time Picker

22 May, 2009 - 04:13 AM
Post #2

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,846



Thanked: 205 times
Dream Kudos: 500
Expert In: .NET

My Contributions
you could look into using the AJAX Calendar.

http://www.asp.net/AJAX/AjaxControlToolkit...r/Calendar.aspx

User is offlineProfile CardPM
+Quote Post

Jayman

RE: Date Time Picker

22 May, 2009 - 11:38 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,570



Thanked: 237 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Add a Calendar control to the page and use the SelectionChanged event to get the date and put it in a TextBox when the user selects a date.
User is offlineProfile CardPM
+Quote Post

woodjom

RE: Date Time Picker

29 May, 2009 - 01:36 PM
Post #4

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 366



Thanked: 15 times
My Contributions
Ok, so what if i have 3 text box with 3 buttons associated with them but only one calendar control.

I click the button it makes the calendar visible, at which point the user selects a date and its supposed to go to the text box.

Catch is...i dont have a calendar for each text box...just 1 calendar to feed 3 text boxes. I created a page-level variable...
CODE

Dim calTextBox As TextBox


which upon one of the buttons click does...
CODE

calTextBox = txtNewProjectStart
clndrNewProject.Visible = True


Then the user causes said event to happen...
CODE

Protected Sub clndrNewProject_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles clndrNewProject.SelectionChanged
  calTextBox.Text = clndrNewProject.SelectedDate.ToString
  clndrNewProject.Visible = False
End Sub


But the catch is for some reason the variable calTextBox is not carrying the object reference of the output text box. Keep getting a NullReferenceException.

I tried doing...
CODE

Dim calTextBox As New Web.UI.WebControls.TextBox


except the data goes no where but everything in the calendar event completes.

Everything above is on the same form and is in the same view of a multiview.

User is offlineProfile CardPM
+Quote Post

Jayman

RE: Date Time Picker

29 May, 2009 - 07:55 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,570



Thanked: 237 times
Dream Kudos: 500
Expert In: Everything

My Contributions
First off keep in mind that every time the page posts back everything is re-instantiated including the page itself. This means that your TextBox control is being created each time the page posts back. And since you are not using the New keyword you are getting the NullReferenceException.

I would suggest an alternative to the method you are attempting to use. Since you are only using one calendar control to populate three different textboxes, you need to know which button that is associated to each textbox initiated the Click event. Store the name of the button in a Session variable and then check that value in the SelectionChanged event of the calendar control.

To illustrate this, here is an example:

Here is the code for the page itself. 3 textboxes, 3 buttons, and 1 calendar control.
HTML
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtDate1" runat="server"></asp:TextBox>
<asp:Button ID="btnDate1" runat="server" Text="Date1" /><br />
<asp:TextBox ID="txtDate2" runat="server"></asp:TextBox>
<asp:Button ID="btnDate2" runat="server" Text="Date2" /><br />
<asp:TextBox ID="txtDate3" runat="server"></asp:TextBox>
<asp:Button ID="btnDate3" runat="server" Text="Date3" /><br />
<asp:Calendar ID="Calendar1" runat="server" Visible="false"></asp:Calendar>
</div>
</form>
</body>
</html>


Each button calls the same event. First create a button in the event and then cast the sender object to the correct type. The sender object will tell us which button called the event. Once we have the correct button, store the ID in a Session variable and then show the calendar.
CODE

    Protected Sub btnDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDate1.Click, btnDate2.Click, btnDate3.Click
        'create a new button
        Dim clickedButton As Button
        'cast the sender to the correct type, a Button
        clickedButton = CType(sender, Button)
        'store the Button ID in a Session variable
        Session("ButtonClicked") = clickedButton.ID
        'show the calendar
        Calendar1.Visible = True

    End Sub


Now in the SelectionChanged event of the calendar get the value stored in the Session variable and use it to determine the logical path to follow and put the date in the correct textbox. Lastly, hide the calendar control again until the next button is clicked.
CODE

    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        'verify that a Session variable is valid before attempting to use it
        If Session("ButtonClicked") <> Nothing Then
            'now check the session variable for the ID of the button
            'this will indicate which textbox to put the date into
            If Session("ButtonClicked").ToString().Equals("btnDate1") Then
                txtDate1.Text = Calendar1.SelectedDate.ToString()
            ElseIf Session("ButtonClicked").ToString().Equals("btnDate2") Then
                txtDate2.Text = Calendar1.SelectedDate.ToString()
            Else
                txtDate3.Text = Calendar1.SelectedDate.ToString()
            End If

        End If
        'hide the calendar
        Calendar1.Visible = False

    End Sub


I hope that helps explain a fairly simple method of accomplishing the task you desire.
User is offlineProfile CardPM
+Quote Post

woodjom

RE: Date Time Picker

1 Jun, 2009 - 07:10 AM
Post #6

D.I.C Regular
Group Icon

Joined: 8 May, 2008
Posts: 366



Thanked: 15 times
My Contributions
Yeah i keep forgetting about the postback event. As well, forgot about the session variables. Looks like ill be creating some variables in the session_start event in the app.config.

I have alot more togo, but thanx for bringing that old strategy back, havent used session variables since i started playing with PHP and then moved to ASP.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 07:52PM

Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month