2013-05-12 30 views
1

我正在寫一個程序,其中用戶有3個下拉框來輸入日期,月份和年份。在用戶選擇我連接它們的值並檢查有效時默認情況下,當頁面加載時,我需要爲每個放置框分配當前日期,月份和年份。然後我檢查日期的有效性並將值傳遞給數據庫。如何將值賦給DropBox文本propery?

我的問題是,當我在加載頁面時將值分配給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> 

同樣,如果我在加載頁面時未將默認值分配給框,則它完美地工作。如果我這樣做,這些值是固定的沒有母校你選擇什麼

的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" 
+0

使用** findstringexact **首先得到索引... – matzone 2013-05-12 06:24:28

+0

whattringegexact是什麼?原諒我的無知。 – meks 2013-05-12 06:34:36

+0

組合框中沒有這種方法嗎? – matzone 2013-05-12 06:43:42

回答

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

爲什麼我要把它們帶到那裏?它不會被更新。會嗎?我到底應該放什麼? – meks 2013-05-12 06:36:12

+0

好吧,讓我試着把它們放在Not.IsPostBack中。只需一秒... – meks 2013-05-12 06:39:11

+0

應該爲你工作。 – 2013-05-12 06:43:43