Click here to Skip to main content
15,896,444 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
i am trying to set current date in dropdown list in asp.net.. i have tried with ddldate.selectedvalue = system.datetime.now, also, ddldate.text = system.datetime.now.. its not working.. can anyone tell how to set value in dropdownlist.. i couldnot get the exact answer to resolve my problem in search engine.. any help??
Posted
Comments
Karthik_Mahalingam 21-Jan-14 1:41am    
can you post your code..
Codes DeCodes 21-Jan-14 1:45am    
ddlLeaveStartdate.SelectedValue = System.DateTime.Now.ToString();
Karthik_Mahalingam 21-Jan-14 1:46am    
i mean the ddlLeaveStartdate source ??
Codes DeCodes 21-Jan-14 1:50am    
ddl takes system datetime..
Karthik_Mahalingam 21-Jan-14 1:52am    
ddl will contain only one date ??

if you dropdownlist contains only one item
then this will help you..

C#
ddlLeaveStartdate.Items.Add(DateTime.Now.ToString("MM/dd/yyyy"));
 
Share this answer
 
Comments
Codes DeCodes 21-Jan-14 2:08am    
thanks karthik...
Karthik_Mahalingam 21-Jan-14 3:29am    
welcome:)
if your issue is resolved, pls close this post..
Use the following syntax...
ddldate.Items.FindByText(system.datetime.now).Selected=true
 
Share this answer
 
You can have a selected value in the dropdownlist once its available in the list.

So first add the list item then set the value.

Something like

SQL
ddl.Items.Add(new ListItem(DateTime.Now.ToString()));

            ddl.SelectedValue = DateTime.Now.ToString();
 
Share this answer
 
Comments
Codes DeCodes 21-Jan-14 2:08am    
thank you JoCodes... solved the problem
JoCodes 21-Jan-14 2:12am    
Welcome Agustus :)

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