簡體   English   中英

如何為DropBox文本屬性分配值?

[英]How to assign value to the DropBox text propery?

我正在編寫一個程序,其中用戶具有3個投遞箱來輸入日期,月份和年份。 用戶選擇值后,我將它們連接起來並檢查是否有效默認情況下,在頁面加載時,我需要為每個投遞箱相應地分配當前日期,月份和年份。 然后,我檢查日期的有效性並將值傳遞給數據庫。

我的問題是,當我在頁面加載時將值分配給DropBoxes的文本時,它們將變為永久性的。 即使更改了索引,傳遞給數據庫的值也是頁面加載時分配給它們的值。

我實際上無法理解我在做什么錯:

這些是我使用的代碼示例:

  1. 我使用以下代碼(在頁面Load事件上)使用當前日期值填充它們:

      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>

同樣,如果我在加載頁面時未將默認值分配給框,則效果很好。 如果我這樣做,這些值是固定的,無論您選擇什么

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" 

我想您在將下拉列表與頁面加載值綁定時沒有使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM