Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1442
  • Last Modified:

Bind a calendar control to a text field.

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
swpa_wnt
Asked:
swpa_wnt
  • 5
  • 4
  • 2
  • +1
1 Solution
 
AerosSagaCommented:
Heres a very good tutorial on this:
http://aspnet.4guysfromrolla.com/articles/030202-1.aspx

Regards,

Aeros
0
 
AerosSagaCommented:
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
 
swpa_wntAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DotNetLover_BaanCommented:
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
 
swpa_wntAuthor Commented:
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
 
DotNetLover_BaanCommented:
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
 
swpa_wntAuthor Commented:
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
 
DotNetLover_BaanCommented:
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
 
jnhorstCommented:
Dumb question, but is the calendar control within the <FORM runat="server"></FORM> tag?

John
0
 
jnhorstCommented:
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
 
DotNetLover_BaanCommented:
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
 
swpa_wntAuthor Commented:
Baan nailed it.  Thanks!  All I was missing was the "Handles Calendar1.SelectionChanged" on the end of the event line.
0
 
DotNetLover_BaanCommented:
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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now