TextChanged Event for Valid Start and End Dates

This seems like a simple problem but Ive been stuck on it for weeks....

I have two textboxes (along with calendar controls) for start and end date.
I also have an asp:dropdownlist box (of client names) which is loaded by a stored procedure; after the user enters a valid start and end date range.

If the end user changes EITHER date field, I would like to refresh the dropdownlist box.
I simply want to add this little attached piece of coding to the text_changed event for both dates.

But, I keep getting conversion errors from textbox to date format, etc.

Is there an easy way to accomplish this task, without using server side validation or required field validators?

Please reply.
Thanks
John
Protected Sub startdate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles startdate.TextChanged
            If IsDate(Convert.ToDateTime(startdate)) = True Then
                ' We're all set - refresh the client drop down!
                Call load_Client()
            Else
                Return
            End If
        End Sub
 
 
        Protected Sub enddate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles enddate.TextChanged
 
            If IsDate(enddate) = True Then
                ' We're all set - refresh the client drop down!
                Call load_Client()
            Else
                Return
            End If
 
        End Sub

Open in new window

jtrapat1Asked:
Who is Participating?
 
jinn_hnnlConnect With a Mentor Commented:
Well I thing JPG file is lighter and good enough to see

The javascript onclick is just a way to prevent user input from client side and improve your page performance (doesnt do the post back everytime text changed regardless to how correct the date format is)

Without it, you still can perform perfectly normal the same scenarios, but the page will be post back and you can validate the date from server (this has a benefit than javascript incase, the local setting format of the client is different).

As I mention, you can still do as you did at the beginning, but add the exception handling. Create a label somewhere
<asp:Label ID="lblMessage" runat="server">

You can improve the performance by put everthing inside an updatepanel.

Hope this help

JINN

Protected Sub startdate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles startdate.TextChanged
	Try
	    If IsDate(Convert.ToDateTime(startdate)) = True Then
		' We're all set - refresh the client drop down!
		Call load_Client()
                lblMessage.Text = "Client data has been loaded"
	    End If
    Catch exp As System.Exception
	  lblMessage.Text = "Your date input is not in a correct format, please try again"
    End try
End Sub

Open in new window

0
 
tiagosalgadoCommented:
In what format is the input dates?
0
 
crazymanCommented:
Try using
DateTime.TryParseExact instead of Convert.ToDateTime ....

and my pet hate

this
If IsDate(enddate) = True Then

should be
If IsDate(enddate)  Then


;-)
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
jinn_hnnlCommented:
Hi,

Are startdate, and enddate your two textboxes? then I guess you have to get the text value out of it to convert:

If IsDate(Convert.ToDateTime(startdate.Text)) = True Then
You might wanna check if the text is not null of empty first before you do the Convert. A better way is you should define a function (call it IsDateTimeInput)
Function
Try
     If IsDate(Convert.ToDateTime(inpuString)) = True Then
                Return true;              
   End If
 Catch exp As System.Exception
     Return false;
End Try

Return false;
End Function


If you dont want to validate your dates on server side, then you have to use javascript for your 2 text box, the way you do it now is using server post back and check it there (this isn't too bad though).

//CheckDateInput is javascript function
startdate.Attributes.Add("onchange", "return CheckDateInput(" + startDate.ClientID + ");"
enddate.Attributes.Add("onchange", "return CheckDateInput(" + endDate.ClientID + ");"

You can define more javascript function to validate this.
And javascript also support exception handle (try catch) and Date.Parse to check the input

Hope this help



0
 
jtrapat1Author Commented:
thanks for the replies-
Start Date and End Date are input text boxes next to two calendar controls;
But, Im just worried that the end user may ignore the calendar and enter the dates themselves.
I guess I could do a format in parentheses next to the textbox (MM/DD/YYYY).
But I want to do some error trapping.

Thanks
John
0
 
jinn_hnnlCommented:
Yes, suggestion is always nice, but when user accidentally or  un-accidentally  input the wrong value, then we dont want to see ugly error page.

Validate them using client javascript is the bestway, otherwise throw exception handling to verify user input. Happy ending ^^

Goodluck

JINN
0
 
tiagosalgadoCommented:
Or to prevent wrong date input, set your textboxs to read-only and assign only the date clicked on your calendar control.
0
 
jtrapat1Author Commented:
Ive included a .bmp to try to make it clearer-
I started with seven input fields to run the report-
Now, since I added the onclick  javascript calendars to help load the textfields for start and end date;
the calendars get the focus and its not as easy to tab thru all the required fields and then hit:
View Report.

The most important thing is that the bottom two drop downs are dependent on the above start and end date textfields
these drop downs below should automatically refresh ANYTIME the start and end dates are changed.

Is there any way to accomplish this - I will try the javascript check for now but i just want to be consistent.
Thanks.

John
gui.bmp
0
 
tiagosalgadoCommented:
"Now, since I added the onclick  javascript calendars to help load the textfields for start and end date;
the calendars get the focus and its not as easy to tab thru all the required fields and then hit:
View Report."
You cant use TabIndex on your textboxs to prevent that.
 
0
 
jtrapat1Author Commented:
Thanks everyone for the help--

I have a lot of it working but a new problem that I think can be fixed.
Now that I covered the case where the user enters a date directly into the "asp:textbox", autopostback is set to true and therefore catches the mistake right away.....good.
(attached is the final code i ended up using.)

Now, I also have href:javascript popup calendars to allow the user to select a date.
The date is selected from the calendar and passed to the start and end date text fields ok but the page does not submit and therefore cannot be validated on the fly like the other validation.
(that code is attached first.)

My question is: can't I force a page submit so that the data will be validated?
Something like: "document.forms[0].submit();"

and the second part of the question is:
where would this code go?
Inside my little calendar popup? After I select the date?

Reply when you can.
John
<td style="width: 340px; height: 30px;">
                        <b><font face="arial" size="2">Start Date:</font></b>
                        <asp:TextBox ID="startdate" runat="server" Width="120px" Text="" TabIndex="1" AutoPostBack="true"></asp:TextBox>
                        <a href="javascript:;" onclick="window.open('PopUp.aspx?textbox=startdate','cal','width=250,height=225,left=270,top=180')">
                            <img alt="" src="images/cal.gif" width="16" height="16" /></a> <b><font face="arial"
                                size="2">(MM/DD/YYYY) </font></b>
                    </td>
 
----------------------------------
Protected Sub startdate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles startdate.TextChanged
 
             If enddate.Text <> "" And startdate.Text <> "" Then
                Try
                    If IsDate(Convert.ToDateTime(startdate.Text)) And _
                        IsDate(Convert.ToDateTime(enddate.Text)) Then
 
                        ' We're all set - refresh the client drop down!
                        Call load_Client()
                        lblMessage.Text = "Client Data Has Been Loaded."
                    End If
                Catch exp As System.Exception
                    lblMessage.Text = "Your Start Date Input Is Not in a correct format, please try again."
                End Try
            End If
        End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.