如何为DropBox文本属性赋值?

时间:2013-05-12 06:10:00

标签: asp.net vb.net

我正在编写一个程序,用户有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个包含月份数字值和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>
    

    同样,如果我在加载页面时没有为框分配默认值,那么它可以完美地工作。如果我这样做,那些价值是固定的,无论你选择什么

    comparevalidator:

    <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" 
    

1 个答案:

答案 0 :(得分: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