In the .Net paradigm, programmatic actions are performed using events, just like
VB has always used (at least in forms programs). In the code you originally posted you already have an event handler:
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Your method "Page_Load" handles the "Load" event of that method's parent class's base class ("MyBase"). "Page_Load()" lives inside of a class that is derived (we say "inherits") from System.Web.UI.Page. This may be transparent to you if you are programming with in-line code in your ASPX file. The System.Web.UI.Page class raises an event at some point when it loads, hence "Load". Your ASPX inherits from the "Page" class so you can refer to it by referencing "MyBase" because the base class of your ASPX is System.Web.UI.Page.
Every web control has their own events. Your dropdownlist and listbox have several events. When you create a dropdownlist in the ASPX you can assign functions to those events.
<asp:DropDownList Runat="server" OnSelectedIndexChanged="" />
Here's a really simple example I just cooked up:
Code:
<%@ Page %>
<script language="vb" runat="server">
Public Sub myDDL_OnChanged _
(sender As Object, e As System.EventArgs)
lblResult.Text = "You selected " & myDDL.SelectedItem.Text _
& " in the dropdown list!"
End Sub
Public Sub myListBox_OnChanged _
(sender As Object, e As System.EventArgs)
lblResult.Text = "You selected " & myListBox.SelectedItem.Text _
& " in the listbox!"
End Sub
</script>
<html>
<body>
<form runat="server" method="post">
<asp:DropDownList Runat="server" ID="myDDL"
AutoPostBack="True"
OnSelectedIndexChanged="myDDL_OnChanged"
>
<asp:ListItem Value="1">One</asp:ListItem>
<asp:ListItem Value="2">Two</asp:ListItem>
<asp:ListItem Value="3">Three</asp:ListItem>
<asp:ListItem Value="4">Four</asp:ListItem>
</asp:DropDownList>
<asp:ListBox Runat="server" ID="myListBox"
AutoPostBack="True"
OnSelectedIndexChanged="myListBox_OnChanged"
>
<asp:ListItem Value="1">One</asp:ListItem>
<asp:ListItem Value="2">Two</asp:ListItem>
<asp:ListItem Value="3">Three</asp:ListItem>
<asp:ListItem Value="4">Four</asp:ListItem>
</asp:ListBox>
<asp:Label Runat="server" ID="lblResult" />
</form>
</body>
</html>
What you can do is take each chunk of code that you want to have run when a particular thing happens (event), and create handlers for that event, then assign the handler to the event of that control. So your DDL will have (just like in my example) it's OnSelectedIndexChanged event handled by one method, and your listbox will have one as well.
Here's what you should shoot for...
Code:
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
calHire.Visible = False
lstEmployees.Visible = False
End If
End Sub
Sub pickMonth_OnChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
lstEmployees.Visible = True
selectedMonth = pickMonth.SelectedItem.Value
Select Case selectedMonth
Case "aug"
startDate = #8/1/2003#
endDate = #8/31/2003#
Case "sept"
startDate = #9/1/2003#
endDate = #9/30/2003#
Case "oct"
startDate = #10/1/2003#
endDate = #10/31/2003#
Case "nov"
startDate = #11/1/2003#
endDate = #11/30/2003#
Case "dec"
startDate = #12/1/2003#
endDate = #12/31/2003#
End Select
Label1.Text = startDate
Label2.Text = endDate
myDataFiller()
lstEmployees.DataSource = objDataSet
lstEmployees.DataTextField = "event"
lstEmployees.DataBind()
End Sub
Sub lstEmployees_OnChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim datHireDate As Date
If lstEmployees.SelectedIndex > -1 Then
datHireDate = Convert.ToDateTime(objDataSet.Tables("dtEmployees").Rows(lstEmployees.SelectedIndex)("event_date"))
lblSelectedDate.Text = objDataSet.Tables("dtEmployees").Rows(lstEmployees.SelectedIndex)("event")
calHire.VisibleDate = datHireDate
calHire.SelectedDate = datHireDate
lblSelectedDate.Text &= " will be " & datHireDate
calHire.Visible = True
Label3.Text = lstEmployees.SelectedItem.Text
End If
End Sub
Sub myDataFiller()
Dim strConnection As String = ConfigurationSettings.AppSettings("datesdb")
Dim objConnection As New OleDbConnection(strConnection)
'Dim strSQL As String = "SELECT event_date, event, event_time, location FROM Fall " & _
'"ORDER BY event_date;"
Dim strSQL As String = "SELECT Event_date, event, event_time, location FROM Fall " & _
"WHERE (event_date BETWEEN #" & startDate & "# AND #" & endDate & "#)" & _
"ORDER BY event_date ASC"
Dim objAdapter As New OleDbDataAdapter(strSQL, objConnection)
objAdapter.Fill(objDataSet, "dtEmployees")
End Sub
This should get you started. Something important to note with this methodology. You'll notice now that the listbox is only bound to the data when you change the month dropdown list. And I JUST reallized that this is the core of your problem...
lstEmployees.DataBind()
...
If lstEmployees.SelectedIndex > -1 Then
When you do "DataBind" I think you are loosing the "SelectedItem" item of your listbox. That's why you never get SelectedIndex > -1! One surefire way to test this is that when you click on a listbox item, the page posts back and that item you clicked on is NOT selected.
Sorry it took so long to come up with that answer.But perhaps now you have a better grasp of how .Net is working.
Peter