我正在VS 2008中开发一个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>
......这是一个非常基本的问题。此代码目前设置了我想要的下拉列表,但只是没有返回选定的月份和年份。我该如何解决这个问题?请注意,Main2 sub适用于PostBack = True。
答案 0 :(得分:2)
您正在Main2
(在PostBack中)绑定DropDownList。这可以防止触发事件并覆盖SelectedValue。
如果您再次在dEndMonth.SelectedValue
之前查找DataBind
,则会看到SelectedValue
是正确的。
为什么不使用事件处理程序来处理SelectedIndexChanged - DropDownList事件而不是Main2
- 方法?
<ASP:DropDownList id="dEndMonth" OnSelectedIndexChanged="EndMonthChanged" autopostback="true" runat="Server" width="75"/></td>
和代码隐藏:
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
您应该对用户的操作做出反应,而不是根据您认为用户可以做的事情做出反应,因此请使用事件处理程序。