Solved

Bind a calendar control to a text field.

Posted on 2004-09-13
13
1,435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 17

Expert Comment

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

Regards,

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12046058
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
ID: 12046205
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12046496
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
ID: 12046571
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
ID: 12046678
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
 

Author Comment

by:swpa_wnt
ID: 12046750
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
ID: 12047001
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
ID: 12047027
Dumb question, but is the calendar control within the <FORM runat="server"></FORM> tag?

John
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12047113
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
ID: 12047133
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
ID: 12047205
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
ID: 12047239
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Generate Unique ID in VB.NET 21 128
asp.net uploading picture to a temporary folder 4 32
Adding Rows to Grid 4 26
Ajax calendar distorted 4 13
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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.…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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