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.
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.
list.SelectedIndex = DateTime.Now.Month - 1;
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;
精彩评论