Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在下拉列表中将当前月份和年份设置为默认值?_Asp.net_.net_Vb.net_Datetime - Fatal编程技术网

Asp.net 如何在下拉列表中将当前月份和年份设置为默认值?

Asp.net 如何在下拉列表中将当前月份和年份设置为默认值?,asp.net,.net,vb.net,datetime,Asp.net,.net,Vb.net,Datetime,我想在页面加载期间在下拉列表中选择当前月份和年份。 我试过了,但是当页面加载时,下拉列表中没有选择该值,这里有什么问题 到目前为止,我已经做到了: <asp:DropDownList ID="ddlMonth" runat="server"> <asp:ListItem Text="January" Value="01"></asp:ListItem> <asp:ListItem Text="February" Value="02"&g

我想在页面加载期间在下拉列表中选择当前月份和年份。 我试过了,但是当页面加载时,下拉列表中没有选择该值,这里有什么问题

到目前为止,我已经做到了:

 <asp:DropDownList ID="ddlMonth" runat="server">
    <asp:ListItem Text="January" Value="01"></asp:ListItem>
    <asp:ListItem Text="February" Value="02"></asp:ListItem>
    <asp:ListItem Text="March" Value="03"></asp:ListItem>
    <asp:ListItem Text="April" Value="04"></asp:ListItem>
    <asp:ListItem Text="May" Value="05"></asp:ListItem>
    <asp:ListItem Text="June" Value="06"></asp:ListItem>
    <asp:ListItem Text="July" Value="07"></asp:ListItem>
    <asp:ListItem Text="August" Value="08"></asp:ListItem>
    <asp:ListItem Text="September" Value="09"></asp:ListItem>
    <asp:ListItem Text="October" Value="10"></asp:ListItem>
    <asp:ListItem Text="November" Value="11"></asp:ListItem>
    <asp:ListItem Text="December" Value="12"></asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="ddlYear" runat="server">
    <asp:ListItem Text="2010" Value="2010"></asp:ListItem>
    <asp:ListItem Text="2011" Value="2011"></asp:ListItem>
    <asp:ListItem Text="2012" Value="2012"></asp:ListItem>
    <asp:ListItem Text="2013" Value="2013"></asp:ListItem>
    <asp:ListItem Text="2014" Value="2014"></asp:ListItem>
    <asp:ListItem Text="2015" Value="2015"></asp:ListItem>
</asp:DropDownList>

您需要在dropdownlist上设置SelectedValue属性,而不是我认为的列表项

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        'Select Current Month and Year to dropdowns
        ddlYear.ClearSelection()
        ddlMonth.ClearSelection()
        ddlYear.SelectedValue = ddlYear.Items.FindByText(Format(Now, "yyyy")).Value;
        ddlMonth.SelectedValue = ddlMonth.Items.FindByText(Today.ToString("MMMM")).Value;
    End If  
End Sub

您需要在dropdownlist上设置SelectedValue属性,而不是我认为的列表项

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        'Select Current Month and Year to dropdowns
        ddlYear.ClearSelection()
        ddlMonth.ClearSelection()
        ddlYear.SelectedValue = ddlYear.Items.FindByText(Format(Now, "yyyy")).Value;
        ddlMonth.SelectedValue = ddlMonth.Items.FindByText(Today.ToString("MMMM")).Value;
    End If  
End Sub