如何实现VB.NET DropDownList?

如何实现VB.NET DropDownList?,vb.net,drop-down-menu,Vb.net,Drop Down Menu,我正在VS2008中开发一个VB.NET ASPX文件。但是,检索所选输入参数时遇到问题。这是一个非常简单的问题。这是我目前的代码: <html> <SCRIPT LANGUAGE="VB" RUNAT="Server"> Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) If Not IsPostBack Then Ma

我正在VS2008中开发一个VB.NET ASPX文件。但是,检索所选输入参数时遇到问题。这是一个非常简单的问题。这是我目前的代码:

<html>
    <SCRIPT LANGUAGE="VB" RUNAT="Server">
        Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

            If Not IsPostBack Then
                Main()
            Else
                Main2()
                If part_transfer.Value.Trim() <> "" Then
                    ShowChart()
                    Panel1.Visible = False
                    Panel2.Visible = True
                End If
            End If

        End Sub

Sub Main() 
'*** Query database and get arrays for the chart and bind query results to datagrid  

Dim YearDate As Date = "1/1/2010"
Dim arrYear As New ArrayList()
While YearDate <= Today
arrYear.Add(YearDate.ToString("yyyy"))
YearDate = YearDate.AddYears(1)
End While

dYear.DataSource = arrYear
dYear.DataBind()
dYear.SelectedValue = Today.ToString("yyyy")

Dim ListMonth As Date = "1/1/2010"
Dim arrListMonth As New ArrayList()
While ListMonth <= "12/1/2010"
arrListMonth.Add(ListMonth.ToString("MMM"))
ListMonth = ListMonth.AddMonths(1)
End While

dEndMonth.DataSource = arrListMonth
dEndMonth.DataBind()

dEndMonth.SelectedValue = Today.ToString("MMM")

Response.Write("Main " & dEndMonth.SelectedValue & "<br>")...
        End Sub

        Sub Main2()
        '*** Query database and get arrays for the chart and bind query results to datagrid  
            Dim YearDate As Date = "1/1/2010"
            Dim arrYear As New ArrayList()

            Dim TextSearch As String 
            TextSearch = dTextSearch.Text

            While YearDate <= Today
                arrYear.Add(YearDate.ToString("yyyy"))
                YearDate = YearDate.AddYears(1)
            End While

            dYear.DataSource = arrYear
            dYear.DataBind()

            Dim ListMonth As Date = "1/1/2010"
            Dim arrListMonth As New ArrayList()
            While ListMonth <= "12/1/2010"
                arrListMonth.Add(ListMonth.ToString("MMM"))
                ListMonth = ListMonth.AddMonths(1)
            End While

            dEndMonth.DataSource = arrListMonth
            dEndMonth.DataBind()

            Response.Write("Main2 " & dEndMonth.SelectedValue & "<br>")
...
        <form runat="Server" method="post" id="Form1">
            <div style="font-size:18pt; font-family:verdana; font-weight:bold; color:#336699">
               Parts Watch List
            </div>
            <br />
             <br />
                    <table>
                <tr><th>Year</th><th>Ending Month</th></tr>
                <tr>
                    <td><ASP:DROPDOWNLIST id="dYear" runat="Server" autopostback="true" /></td>
                    <td><ASP:DROPDOWNLIST id="dEndMonth" runat="Server" autopostback="true" width="75"/></td>
                    <td><ASP:TEXTBOX id="dTextSearch" OnTextChanged="dBtn_TextChanged" columns="2" MaxLength="30" Text="" runat="Server" autopostback="true" Width="150" /></td>
                    <td><ASP:BUTTON id="dBtn" Text="Search" OnClick="dBtn_Click" runat="Server" autopostback="true" width="100"/></td>
                </tr>
            </table>

子页面加载(ByVal发送方作为对象,ByVal E作为事件参数)
如果不是的话,我会回来的
Main()
其他的
main 2()
如果部分_transfer.Value.Trim()“”则
图表()
面板1.可见=错误
Panel2.Visible=True
如果结束
如果结束
端接头
副标题()
'***查询数据库并获取图表的数组,并将查询结果绑定到datagrid
Dim YearDate As Date=“2010年1月1日”
作为新的ArrayList()的一年

当YearDate时,您也在
Main2
(回发)中绑定DropDownList。防止触发事件并覆盖SelectedValue的

如果在重新绑定数据之前查找
dEndMonth.SelectedValue
,您会发现
SelectedValue
是正确的

为什么不为DropDownList的-event使用事件处理程序而不是
Main2
-方法

<ASP:DropDownList id="dEndMonth" OnSelectedIndexChanged="EndMonthChanged" autopostback="true" runat="Server" width="75"/></td>

你应该对用户的行为做出反应,而不是你认为用户可以做什么,因此使用事件处理程序。

非常感谢你,Tim!你的解决方案立刻对我起了作用。而且节省了我很多时间!
Protected Sub EndMonthChanged(sender as Object, e as EventArgs)
    Dim dEndMonth = DirectCast(sender, DropDownList)
    'get selected value etc. and DataBind it AFTERWARDS when needed or whatever...'
End Sub