Suppose i have a dropdownlist in which whole months are there in that dropdown.I want that on page load dropdownlist select current month automatically(means with the help of back end code using C#). so how to do this ? please tell me.
views:
35answers:
3
+1
A:
Assuming that your drop down contains an ordered list of months, where the first (index 0) is January, and the last (index 11) is December:
myDropDown.SelectedIndex = DateTime.Now.Month - 1;
If you have an placeholder option (eg "select value from list") as the first option, simply strip the - 1
part to have the correct month selected.
Jørn Schou-Rode
2010-09-29 12:24:58
It is not working sir?
Shalni
2010-09-29 12:28:52
What a shame :(
Jørn Schou-Rode
2010-09-29 12:32:24
sorry sir, it is working. I have done mistake.
Shalni
2010-09-29 12:42:18
A:
OK, so I'm assuming in your markup you have something like:
<asp:dropdownlist runat="server" id="MonthDropDownList">
<asp:ListItem Text="January" Value="1">
....
<asp:ListItem Text="December" Value="12">
</asp:DropDownList>
Then you'd want something like:
MonthDropDownList.Items.FindByValue(DateTime.Today.Month).Selected = true;
PhilPursglove
2010-09-29 12:27:30