Solved

TextChanged Event for Valid Start and End Dates

Posted on 2008-10-14
10
637 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:jtrapat1
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22716534
In what format is the input dates?
0
 
LVL 13

Expert Comment

by:crazyman
ID: 22716584
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
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22718765
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
 

Author Comment

by:jtrapat1
ID: 22719017
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
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22719076
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22719143
Or to prevent wrong date input, set your textboxs to read-only and assign only the date clicked on your calendar control.
0
 

Author Comment

by:jtrapat1
ID: 22719864
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
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 500 total points
ID: 22720019
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
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22720210
"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
 

Author Comment

by:jtrapat1
ID: 22721452
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

9 Experts available now in Live!

Get 1:1 Help Now