Solved

TextChanged Event for Valid Start and End Dates

Posted on 2008-10-14
10
638 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i determine which dropdownlist caused the postback on Page Load event ? 1 40
Different Delete Messages 7 37
Not showing page correctly 3 29
Host asp.net pages 5 25
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

12 Experts available now in Live!

Get 1:1 Help Now