tags:

views:

35

answers:

3

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.

+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
It is not working sir?
Shalni
What a shame :(
Jørn Schou-Rode
sorry sir, it is working. I have done mistake.
Shalni
+1  A: 
list.SelectedIndex = DateTime.Now.Month - 1;
Jackson Pope
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