I am developing a VB.NET ASPX file in VS 2008. I am having trouble retrieving the selected input parameters however. This is real simple problem. Here is my current code:
<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>
…This is a really basic question. This code currently sets the dropdownlist like I want, but just doesn’t return selected month and year. How can I fix this?Note that Main2 sub is for PostBack = True.
You are binding the DropDownList also in
Main2(in PostBack). That prevents events from being triggered and overwrites the SelectedValue.If you would look for
dEndMonth.SelectedValuebefore youDataBindit again, you would see that theSelectedValueis correct.Why aren’t you using an event-handler for the SelectedIndexChanged-Event of the DropDownList instead of your
Main2-method?and in codebehind:
You should react on the user’s action and not on what you think what the user could have done, therefore use event-handlers.