Click here to Skip to main content
15,885,074 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dropdown list of date.

when the page load I fill the list -

C#
int endYear = DateTime.Now.Year + 1;
          for (int i = 1; i < 31; i++)
          {
              DDLday.Items.Add(i.ToString());
          }
          for (int i = 1; i < 13; i++)
          {
              DDLmonth.Items.Add(i.ToString());
          }
          for (int i = endYear; i > 1920; i--)
          {
              DDLYear.Items.Add(i.ToString());
          }


and put the date from the database
C#
DateTime bday = DateTime.Parse(currentUser.Date.ToString());
DDLday.SelectedValue = bday.Day.ToString();
DDLmonth.SelectedValue = bday.Month.ToString();
DDLYear.SelectedValue = bday.Year.ToString();



and when I want to edit the date in the data -

C#
Date = DateTime.Parse(DDLday.Text + "/" + DDLmonth.Text + "/" + DDLYear.Text),



but the problem is because I use .selectedvalue the text not changed, how can I edit the date?

What I have tried:

I am beginner and I don't know what to do
Posted
Updated 10-Dec-19 9:20am
Comments
MadMyche 9-Dec-19 17:54pm    
Lets start with What Language are you working in?
Member 14683862 10-Dec-19 6:59am    
c#
Maciej Los 10-Dec-19 4:12am    
Using ToString() method is redundant!
You have to work on proper data types.
Imagine: "2019-12-10" and "2019.12.10" and "2019/12/10" are string representation of dates, accordingly in Polish, German and English notations. They are equal (as a date), but its string representation - NOT!

Please, read my comment to the question first!

Here is an example of what i've stated in the comment.
C#
System.Globalization.CultureInfo[] cis = new System.Globalization.CultureInfo[]
	{
		new System.Globalization.CultureInfo("PL-pl"),
		new System.Globalization.CultureInfo("DE-de"),
		new System.Globalization.CultureInfo("EN-us")
	};
	
DateTime dt = DateTime.Today;
foreach(System.Globalization.CultureInfo ci in cis)
	Console.WriteLine(dt.ToString("d", ci));


As you can see, i'm using one date, but result is:
2019-12-10
10.12.2019
12/10/2019


More:
Standard Date and Time Format Strings | Microsoft Docs[^]
Custom date and time format strings - .NET | Microsoft Docs[^]

Additional note: Do not create a date like this:
C#
DateTime dt = DateTime.Parse(DDLday.Text + "/" + DDLmonth.Text + "/" + DDLYear.Text),

Rather then it, use:
C#
DateTime dt = new DateTime(DDLday.SelectedValue, DDLmonth.SelectedValue, DDLYear.SelectedValue),


Finally, i'd suggest to read this: asp.net - SelectedValue vs SelectedItem.Value of DropDownList - Stack Overflow[^]
 
Share this answer
 
Comments
Member 14683862 10-Dec-19 7:04am    
first of all thank you,

but the most question is -
how can i put when the page load DDLday.SelectedValue = 14;
and after the user change the selctedvalue and submit it will change?
Maciej Los 10-Dec-19 11:27am    
Try this:
//changing index
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(<value>));
//or
//DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>));

//changing value
DropDownList1.Items.FindByText("<Text>").Selected = true ;
//or
//DropDownList1.Items.FindByValue("<value>").Selected = true ;
Member 14683862 10-Dec-19 15:13pm    
thank you...
Maciej Los 12-Dec-19 12:56pm    
You're very welcome.
i put in the code

C#
if(!IsPostBack)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900