开发者

How to select current month from dropdownlist using C# with the help of back end code?

开发者 https://www.devze.com 2023-01-18 05:37 出处:网络
Suppose i have a dropdownlist in which whole months are there inthat dropdown.I want that on page load dropdownlist select current month automatically(means with the help of back end code using C#). s

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消