Adding date automatically to textbox when i select an item from drop down list

Posted on 2012-03-19
Last Modified: 2012-03-21
I have a drop down list which has 3 items in it, i also have two Textboxes, Textbox1 and Textbox2. I want that when I select item1 from Dropdownlist and enter a date in Textbox1, The date in Textbox2 should be changed to 3years ahead of the date in Textbox1. Similarly when i select Item2 in dropdownlist and enter a date in Textbox1 the date in textbox2 should be automatically changed to (Textbox1 date+5years). I have the date in MM/DD/YYY format. Any ideas how to achive it using C# code. I know that I can use a stored procedure for this but I want to accomplish this using C# code.
Question by:skolanti
  • 3
  • 3
LVL 29

Expert Comment

ID: 37740005

Author Comment

ID: 37742735
Hi, I am a beginner in ASP.Net web development, Can you provide me with sample code according to my Scenario?
LVL 29

Accepted Solution

QPR earned 500 total points
ID: 37744496
aspx page:

 <asp:DropDownList ID="DropDownList1" runat="server"
        <asp:ListItem Value="3">3 years</asp:ListItem>
        <asp:ListItem Value="5">5 years</asp:ListItem>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

aspx.cs page

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

        if (TextBox1.Text != "")
            DateTime GivenDate = Convert.ToDateTime(TextBox1.Text);

            if (DropDownList1.SelectedValue == "3")
                TextBox2.Text = GivenDate.AddYears(3).ToString();
            if (DropDownList1.SelectedValue == "5")
                TextBox2.Text = GivenDate.AddYears(5).ToString();
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.


Author Comment

ID: 37747475
Thanks, This code worked but I had to make some changes based on my program.

Author Closing Comment

ID: 37747488
Thanks, this code worked fine but I had to make some changes based on my Program logic.
LVL 29

Expert Comment

ID: 37749488
B is a bit unfriendly, I gave you the entire code needed to do what you needed... but thanks anyway

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
fomat Json objects 6 28
Sum Column in GridView 3 43
DateTimepicker 4 33
Export import database 4 42
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following:…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: : Go t…

912 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

28 Experts available now in Live!

Get 1:1 Help Now