Solved

Bind a calendar control to a text field.

Posted on 2004-09-13
13
1,430 Views
Last Modified: 2008-01-09
I have the following code:

    Private Sub calEndCalendar_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txtEndDate.Text = calEndCalendar.SelectedDate()
    End Sub

    Private Sub txtStartDate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStartDate.TextChanged
        txtStartDate.Text = calStartCalendar.SelectedDate()
    End Sub

What I'm trying to do is make it so that when a user clicks on a date on the calendar, .net puts that date into the associated text field, however it's not working.  Can someone fix this for me?

Thanks,
Steve
0
Comment
Question by:swpa_wnt
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
Heres a very good tutorial on this:
http://aspnet.4guysfromrolla.com/articles/030202-1.aspx

Regards,

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
Now all that remains is the Calendar1_SelectionChanged event handler, where we need to emit client-side JavaScript code to update the form and close the pop-up. While we could simply use Response.Write's, the output would appear before any other HTML content (including the <html> tag). So, to make our HTML "correct," we'll use a literal control inbetween the <head> ... </head> tags. Creating this literal control is easy enough:

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

We will use this literal control to hold our JavaScript after we create it. Now lets look at creating the JavaScript to pass the value back to the form. The first thing we will do is create a string, strJScript, to hold our JavaScript code. Next, we will build up this string so that it's end contents set the form's text box's value to the selected date and the window is closed. This is accomplished with the following code in the Calendar1_SelectionChanged event handler.

Private Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs)
  Dim strjscript as string = "<script language=""javascript"">"
  strjscript &= "window.opener." & _
        Httpcontext.Current.Request.Querystring("formname") & ".value = '" & _
        Calendar1.SelectedDate & "';window.close();"
  strjscript = strjscript & "</script" & ">" 'Don't Ask, Tool Bug
   
  Literal1.Text = strjscript  'Set the literal control's text to the JScript code
End Sub

Here we round out the event handler by setting the text of our literal to the dynamically generated client-side JavaScript code. This has the effect of executing this client-side JavaScript code when the page is rendered. So when a user selects a date this event will fire and the JavaScript will be created. As the page is rendered the JavaScript will be run, the value of the selected date transferred to the form's text box, and the calendar page closed. Neat!
0
 

Author Comment

by:swpa_wnt
Comment Utility
That involved calendar popups.  I'm wanting the calendars to be on the page, which I've already got.  I just need to get the selected date into the text field.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
See, when you click on a date in ur calendar control, it posts back. It reloads the page. So definitely it involves Page_Load event. First try,
                txtEndDate.Text = calEndCalendar.SelectedDate.ToString("MM/dd/yyyy")
                txtStartDate.Text = calStartCalendar.SelectedDate.ToString("MM/dd/yyyy")
If it doesn't work, let me see what u have in ur Page_Load.
-Baan
0
 

Author Comment

by:swpa_wnt
Comment Utility
Baan - That initially populates "01/01/0001" in  each date field, then you have to double-click on the dates to get them into the fields.  I need just one click to work, and no date popuation on page load.

Here's my page_load event:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim conRequests As SqlConnection
        Dim cmdInsert As SqlCommand
        Dim cmdRequestor As SqlCommand
        Dim cmdImplementationTeam As SqlCommand
        Dim cmdResponseTeam As SqlCommand
        Dim cmdImpactedSystems As SqlCommand
        Dim strInsert As String
        Dim dtrRequestor As SqlDataReader
        Dim dtrImplementationTeam As SqlDataReader
        Dim dtrResponseTeam As SqlDataReader
        Dim dtrImpactedSystems As SqlDataReader
        Dim dtmDate As DateTime

        'Requested date date population.
        dtmDate = DateTime.Now()
        txtDateRequested.Text = dtmDate

        'Connection info
        conRequests = New SqlConnection("***")
        strInsert = "Insert change_requests ( request_date, work_orders ) values ( @request_date, @work_orders )"
        cmdInsert = New SqlCommand(strInsert, conRequests)
        cmdInsert.Parameters.Add("@request_date", txtDateRequested.Text)
        cmdInsert.Parameters.Add("@work_orders", txtWorkOrders.Text)
        conRequests.Open()
        cmdInsert.ExecuteNonQuery()

        'Locations checkboxes.
        If Not IsPostBack Then
            cblLocations.Items.Add("All")
            cblLocations.Items.Add("Gore")
            cblLocations.Items.Add("Jonesboro")
            cblLocations.Items.Add("Springfield")
            cblLocations.Items.Add("Table_Rock")
            cblLocations.Items.Add("Tulsa")
        End If

        'Change Risk checkboxes.
        If Not IsPostBack Then
            cblChangeRisk.Items.Add("Low")
            cblChangeRisk.Items.Add("Med")
            cblChangeRisk.Items.Add("High")
        End If

        'Requestor dropdownlist.
        cmdRequestor = New SqlCommand("select name from wnt_analysts", conRequests)
        dtrRequestor = cmdRequestor.ExecuteReader()
        ddlRequestor.DataSource = dtrRequestor
        ddlRequestor.DataTextField = "name"
        ddlRequestor.DataBind()
        dtrRequestor.Close()

        'Implementation team checkboxes.
        cmdImplementationTeam = New SqlCommand("select name from wnt_analysts where name != '---Select Analyst---'", conRequests)
        dtrImplementationTeam = cmdImplementationTeam.ExecuteReader()
        cblImplementationTeam.DataSource = dtrImplementationTeam
        cblImplementationTeam.DataTextField = "name"
        cblImplementationTeam.DataBind()
        dtrImplementationTeam.Close()

        'Response team checkboxes.
        cmdResponseTeam = New SqlCommand("select name from wnt_analysts where name != '---Select Analyst---'", conRequests)
        dtrResponseTeam = cmdResponseTeam.ExecuteReader()
        cblResponseTeam.DataSource = dtrResponseTeam
        cblResponseTeam.DataTextField = "name"
        cblResponseTeam.DataBind()
        dtrResponseTeam.Close()

        'Impacted Systems listbox.
        cmdImpactedSystems = New SqlCommand("select title from software order by title asc", conRequests)
        dtrImpactedSystems = cmdImpactedSystems.ExecuteReader()
        lstbxImpactedSystems.DataSource = dtrImpactedSystems
        lstbxImpactedSystems.DataTextField = "title"
        lstbxImpactedSystems.DataBind()
        dtrImpactedSystems.Close()

        txtEndDate.Text = calEndCalendar.SelectedDate.ToString("MM/dd/yyyy")
        txtStartDate.Text = calStartCalendar.SelectedDate.ToString("MM/dd/yyyy")

        conRequests.Close()
    End Sub
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
But I see you are using selected date in page_load...
        txtEndDate.Text = calEndCalendar.SelectedDate.ToString("MM/dd/yyyy")
        txtStartDate.Text = calStartCalendar.SelectedDate.ToString("MM/dd/yyyy")

Remove them from page_load... and change two SelectionChanged event of ur calendars...

    Private Sub calEndCalendar_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txtEndDate.Text = calEndCalendar.SelectedDate.ToString("MM/dd/yyyy")
    End Sub

    Private Sub txtStartDate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStartDate.TextChanged
        txtStartDate.Text = calStartCalendar.SelectedDate.ToString("MM/dd/yyyy")
    End Sub

-Baan
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:swpa_wnt
Comment Utility
That's how I had it done originally, that doesn't do anything.  When I click on the dates, the screen flashes, and does nothing.
0
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 100 total points
Comment Utility
did you remove those lines from Page_Load ?

I am doing the same thing, and displaying two dates in two labels. It is working just fine.... This is what I have done..
    Private Sub Calendar1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        Label1.Text = Calendar1.SelectedDate.ToShortDateString
    End Sub
and it is displaying the date in the label after post back.
0
 
LVL 10

Expert Comment

by:jnhorst
Comment Utility
Dumb question, but is the calendar control within the <FORM runat="server"></FORM> tag?

John
0
 
LVL 10

Expert Comment

by:jnhorst
Comment Utility
This is incorrect:

txtStartDate.Text = calStartCalendar.SelectedDate()

I think the VB.NET compiler will let you get away with it, though (the C# compiler errors out).  This is because SelectedDate is a property of the DateTime type, not a method.  Baan had this suggestion:

txtEndDate.Text = calEndCalendar.SelectedDate.ToString("MM/dd/yyyy")

And he's right.  Try that because it is not the same as what you had posted in the original question.  I put a calendar control and textbox on a page and coded just like that and it worked fine.

John
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
Of course jnhorst  , otherwise he couldn't create the handler:
Private Sub txtStartDate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStartDate.TextChanged

-Baan
0
 

Author Comment

by:swpa_wnt
Comment Utility
Baan nailed it.  Thanks!  All I was missing was the "Handles Calendar1.SelectionChanged" on the end of the event line.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
damn... I noticed that... I thought you didn't paste the whole thing and I ignored it. lol... n e way.. good luck.
-Baan
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now