calendar extender

I have 3 text boxes
term
startdate
enddate

I have a calendar extender that populates that startdate.
I need the enddate to auto populate startdate x term with is in months

ie 04/01/10 is selected in startdate and term was 12
enddate will auto popuate with 04/01/11

adamn123Asked:
Who is Participating?
 
Alfred A.Commented:
Try this.  I assumed you have term.Text already

Private Sub startDate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles startDate.TextChanged
     If Len(startdate.Text) > 0 And Len(term.Text) > 0 Then
                Dim StartDate1 As DateTime = CDate(startdate.Text)
                Dim term1 As Integer = CInt(term.Text)
                enddate.Text = StartDate1.AddMonths(term1)
     End If
End Sub
0
 
Alfred A.Commented:
use AddMonths

For example

Dim NewDT As DateTime = Now()
NewDT = NewDT.AddMonths(term)
0
 
Alfred A.Commented:
Dim StartDate as DateTime = Now()
Dim term as Integer = 12
Dim EndDate as DateTime

EndDate = StartDate.AddMonths(term)
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
adamn123Author Commented:
startdate is genorated by a calendar extender.
after the startdate is picked how can I make your code populate the enddate.

I tried in page load but seems like the calendar extender.does not trigger it
0
 
Alfred A.Commented:
In C#

DateTime StartDate = System.Date.Now;
int term = 12;
DateTime EndDate;
EndDate = StartDate.AddMonths(term);
0
 
adamn123Author Commented:
I am using vb.net
sorry should have made that more clear in question
0
 
Alfred A.Commented:
OK.  From the calendar extender let say the textbox it is linked is txtStartDate

Dim StartDate As DateTime = CDate(txtStartDate.Text)
Dim term as Integer = 12
Dim EndDate as DateTime

EndDate = StartDate.AddMonths(term)
0
 
Alfred A.Commented:
And for the term by the way, you could do it this way

Dim term as integer = CInt(txtTerm.text)

You can also use Integer.TryParse to check whether it is really integer or not.
0
 
adamn123Author Commented:
I still not getting the code to trigger, I tried in page load and even in a button  

      Dim StartDate1 As DateTime = CDate(startdate.Text)
        Dim term1 as integer = CInt(term.text)
        Dim EndDate1 As DateTime

        EndDate1 = StartDate1.AddMonths(term1)

I added the 1's as it conflicted with my fields.
don't I need something like enddate.text = ???
0
 
Alfred A.Commented:
What is the value of startdate.Text?
0
 
Alfred A.Commented:
I expect you should have something like this in your source/html for example:

<cc1:CalendarExtender ID="calStartDate" runat="server"
                TargetControlID="startDate" Format="dd MMM yyyy" >
            </cc1:CalendarExtender>
            <asp:TextBox ID="startDate" runat="server" Height="18px" Width="85px"></asp:TextBox>


Is this right?
0
 
adamn123Author Commented:
                                    <asp:TextBox ID="startdate" runat="server" Width="100px" onKeyPress = "javascript: return false;" onPaste = "javascript: return false;"  ></asp:TextBox>
                                    <asp:ImageButton ID="Imagestart" runat="server"
                                        ImageUrl="~/media/SmallCalendar.gif" style="height: 16px" />
                                       
                                <ajaxToolkit:CalendarExtender ID="CalendarExtender3" runat="server"
                                         TargetControlID = "startdate" PopupButtonID="Imagestart"
                                         CssClass="cal_Theme1"></ajaxToolkit:CalendarExtender>
0
 
adamn123Author Commented:
by putting this in a button vb script it will populate the data

        Dim StartDate1 As DateTime = CDate(startdate.Text)
        Dim term1 As Integer = CInt(term.Text)
        enddate.Text = StartDate1.AddMonths(term1)

anyway of making it do it after the startdate is selected by the extender?
0
 
Alfred A.Commented:
I don't know if I understood correctly your last comment but if you selected a date in the calendar, the calendar extender will put the date in the textbox based on the format provided (where is your format by the way?).  If you look at my sample I have a format in there.

And then since the value is in the textbox, you can just access the value using textbox1.text as an example.

I hope this helps.
0
 
adamn123Author Commented:
What I am, trying to say is I put a button beside enddate that says populate

The code behind it is
    Protected Sub LinkButton4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton4.Click
        Dim StartDate1 As DateTime = CDate(startdate.Text)
        Dim term1 As Integer = CInt(term.Text)
        enddate.Text = StartDate1.AddMonths(term1)
    End Sub

This works as you have helped me create it.

My question now is.  when I select the date for the startdate field from the extender I want it to run the following code
        Dim StartDate1 As DateTime = CDate(startdate.Text)
        Dim term1 As Integer = CInt(term.Text)
        enddate.Text = StartDate1.AddMonths(term1)

how do I do this
0
 
Alfred A.Commented:
Correct me if I am wrong,  

1.  You click on startDate textbox, calendar displays.
2.  You selected a date, date populates in startDate textbox
3.  You enter terms in another textbox
4.  You then click on linkbutton beside enddate and executes code by doing postback.  enddate.text is now populated after post back.

This basically answers your question, right?
0
 
adamn123Author Commented:
1.  You enter terms in textbox
2.  You click on startDate textbox, calendar displays.
3.  You selected a date, date populates in startDate textbox, as well as it takes the date selected add the valie that is in the terms textbox field and populates in endDate
0
 
Alfred A.Commented:
Ok.  Try this.  This will generate a postback for the calendar extender and then you can capture the text value in the Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
In your code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Const strKey As String = "CalendarPostback"
        If Not Me.ClientScript.IsClientScriptBlockRegistered(Me.GetType(), strKey) Then
            Dim pbo As PostBackOptions = New PostBackOptions(Me)
            pbo.AutoPostBack = True
            pbo.RequiresJavaScriptProtocol = False
            pbo.PerformValidation = True
            Dim strScript As String = String.Format("function CalendarPostback(e) {{ {0} }}", _
                   Me.ClientScript.GetPostBackEventReference(pbo).ToString)
            Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), strKey, strScript, True)
        End If
    End Sub

In your HTML/Source, add OnClientDateSelectionChanged event in the Calendar Extender

<cc1:CalendarExtender ID="calStartDate" runat="server"
                TargetControlID="startDate" Format="dd MMM yyyy" OnClientDateSelectionChanged="CalendarPostback">
            </cc1:CalendarExtender>
            <asp:TextBox ID="startDate" runat="server" Height="18px" Width="85px"></asp:TextBox>

Open in new window

0
 
adamn123Author Commented:
I put this in page load in code behind

        Const strKey As String = "CalendarPostback"
        If Not Me.ClientScript.IsClientScriptBlockRegistered(Me.GetType(), strKey) Then
            Dim pbo As PostBackOptions = New PostBackOptions(Me)
            pbo.AutoPostBack = True
            pbo.RequiresJavaScriptProtocol = False
            pbo.PerformValidation = True
            Dim strScript As String = String.Format("function CalendarPostback(e) {{ {0} }}", _
                   Me.ClientScript.GetPostBackEventReference(pbo).ToString)
            Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), strKey, strScript, True)
        End If

and put  add OnClientDateSelectionChanged event in the Calendar Extender

works great Thank you
0
 
adamn123Author Commented:
Sorry pasted wrong I put thuis in page load in code behind

       Const strKey As String = "CalendarPostback"
        If Not Me.ClientScript.IsClientScriptBlockRegistered(Me.GetType(), strKey) Then
            Dim StartDate1 As DateTime = CDate(startdate.Text)
            Dim term1 As Integer = CInt(term.Text)
            enddate.Text = StartDate1.AddMonths(term1)
        End If
0
 
Alfred A.Commented:
Hi adamn123,

Did it work?  This should work as I used something like this last year.
0
 
adamn123Author Commented:
thought my way did it but when I first load the page it gives error

when I try your code
        Const strKey As String = "CalendarPostback"
        If Not Me.ClientScript.IsClientScriptBlockRegistered(Me.GetType(), strKey) Then
            Dim pbo As PostBackOptions = New PostBackOptions(Me)
            pbo.AutoPostBack = True
            pbo.RequiresJavaScriptProtocol = False
            pbo.PerformValidation = True
            Dim strScript As String = String.Format("function CalendarPostback(e) {{ {0} }}", _
                   Me.ClientScript.GetPostBackEventReference(pbo).ToString)
            Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), strKey, strScript, True)
        End If

it does not populate anything in enddate
0
 
adamn123Author Commented:
This works

Would this make sense to you

        Const strKey As String = "CalendarPostback"
        If Not Me.ClientScript.IsClientScriptBlockRegistered(Me.GetType(), strKey) Then
            Dim pbo As PostBackOptions = New PostBackOptions(Me)
            pbo.AutoPostBack = True
            pbo.RequiresJavaScriptProtocol = False
            pbo.PerformValidation = True
            Dim strScript As String = String.Format("function CalendarPostback(e) {{ {0} }}", _
                   Me.ClientScript.GetPostBackEventReference(pbo).ToString)
            Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), strKey, strScript, True)
            If Len(startdate.Text) > 0 And Len(term.Text) > 0 Then
                Dim StartDate1 As DateTime = CDate(startdate.Text)
                Dim term1 As Integer = CInt(term.Text)
                enddate.Text = StartDate1.AddMonths(term1)
            End If
        End If
0
 
Alfred A.Commented:
I guess it should work but I believe it would be safer if you inspect the TextChanged event of the textbox and do it inside rather in a page load.

The TextChanged event confirms that something was inserted inside the textbox.  You have to look for any possibility of future enhancement in your code and isolating code logic would make your code less convoluted.
0
 
adamn123Author Commented:
ok how would I go about doing that then?
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.