1

我正在编写一个程序,其中用户有 3 个下拉框来输入日期、月份和年份。在用户选择值后,我将它们连接起来并检查是否有效默认情况下,当页面加载时,我需要相应地为每个下拉框分配当前日期、月份和年份。然后我检查日期的有效性并将值传递给数据库。

我的问题是,当我在加载页面时将值分配给 DropBoxes 的文本时,它们将变为永久性的。即使更改了索引,传递给数据库的值也是加载页面时分配给它们的值。

我实际上无法理解我做错了什么:

这些是我使用过的代码示例:

  1. 我使用以下代码(在页面加载事件上)用当前日期值填充它们:

         Dim CurYear As Integer = DatePart("yyyy", Now)
         Dim CurDate As Integer = DatePart("d", Now)
         Dim CurMonth As String = Format(Today.Date, "MMMM")
         Dim CurDate2 As Integer = DatePart("d", Now)
         Dim CurMonth2 As String = Format(Now, "MM") 
    
         Dates.Text = CurDate
         Monthe.Text = CurMonth
         years.Text = CurYear
         Month2.Text = CurMonth2
         Dates2.Text = CurDate2
    

比我必须同步 2 个包含月份数值和 2 位日期格式的保管箱的选定索引,以形成正确的字符串以检查日期

Protected Sub Months_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Months.SelectedIndexChanged
  Month2.SelectedIndex = Months.SelectedIndex
  TextBox3.Text = years.Text & "-" & Monthes.Text & "-" & Dates.Text
 End Sub

Protected Sub Dates_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Dates.SelectedIndexChanged
    Dates2.SelectedIndex = Dates.SelectedIndex
    TextBox3.Text = years.Text & "-" & Monthes.Text & "-" & Dates.Text
End Sub

这是前端 ASP 代码:

<asp:DropDownList ID="Dates" runat="server" autopostback="true">
                      <asp:ListItem></asp:ListItem>
                      <asp:ListItem>1</asp:ListItem>
                      ...
                      <asp:ListItem>26</asp:ListItem>
                      <asp:ListItem>27</asp:ListItem>
                      <asp:ListItem>28</asp:ListItem>
                      <asp:ListItem>29</asp:ListItem>
                      <asp:ListItem>30</asp:ListItem>
                      <asp:ListItem>31</asp:ListItem>
                    </asp:DropDownList>

                    <asp:DropDownList ID="Months" runat="server" autopostback="true" >
                      <asp:ListItem></asp:ListItem>
                      <asp:ListItem>January</asp:ListItem>
                      ...
                      <asp:ListItem>November</asp:ListItem>
                      <asp:ListItem>December</asp:ListItem>
                    </asp:DropDownList>


                    <asp:DropDownList ID="years" runat="server" autopostback="true" >
                      <asp:ListItem></asp:ListItem>
                    </asp:DropDownList>


                    <asp:DropDownList ID="Dates2" runat="server" AutoPostBack="True">
                      <asp:ListItem></asp:ListItem>
                      <asp:ListItem>01</asp:ListItem>
                      <asp:ListItem>02</asp:ListItem>
                      ....
                      <asp:ListItem>29</asp:ListItem>
                      <asp:ListItem>30</asp:ListItem>
                      <asp:ListItem>31</asp:ListItem>
                    </asp:DropDownList>

                    <asp:DropDownList ID="Month2" runat="server" AutoPostBack="True">
                      <asp:ListItem></asp:ListItem>
                      <asp:ListItem>01</asp:ListItem>
                      ....
                      <asp:ListItem>11</asp:ListItem>
                      <asp:ListItem>12</asp:ListItem>
                    </asp:DropDownList>

同样,如果我在加载页面时没有为框分配默认值,它会完美运行。如果我这样做,无论您选择什么,这些值都是固定的

比较验证器:

<asp:UpdatePanel ID="UpdatePanel19" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox3" ValidationGroup="CompareValidatorDateTest" runat="server"></asp:TextBox>
</ContentTemplate>
       <Triggers>
       <asp:AsyncPostBackTrigger ControlID="Dates" EventName="SelectedIndexChanged" />
       <asp:AsyncPostBackTrigger ControlID="Monthes" EventName="SelectedIndexChanged" />
       <asp:AsyncPostBackTrigger ControlID="years" EventName="SelectedIndexChanged" />
       </Triggers>
       </asp:UpdatePanel>

       <asp:CompareValidator ID="CompareValidator3" Display="dynamic" ControlToValidate="TextBox3"
        Type="Date" Operator="LessThanEqual" Text="Please enter a valid date" runat="server"
        ValidationGroup="CompareValidatorDateTest" 
4

1 回答 1

1

我想您IsPostBack在将下拉列表与页面加载时的值绑定时没有使用属性。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then

      // Bind your dropdown here here

End If
于 2013-05-12T06:24:00.070 回答