Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 9th, 2003, 09:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default AutoPostBack

I have a dropdownlist box with months and then I check for postback and take whatever month is selected and send it to the datafiller sub that returns data to a ListBox based on month selected. The problem is the ListBox, you can click on an item but it just autoposts back and does nothing. My logic might be off. I want the lstEmployees to call the calendar and show the date selected based on what the user clicks in the ListBox.

Here is my 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
        Else
            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()
            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 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
 
Old September 9th, 2003, 11:15 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'm not real clear on what you question is specifically. Regarding the autopostback problem: Do you not want the listbox to cause autopostback? (Set the AutoPostBack property to false.) Or is it that the postback from the listbox is not resulting in something you want to have happen?

Peter
 
Old September 9th, 2003, 12:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

the first post back from the dropdownlist works but when I try the ListBox it posts back but no data is sent to the calendar control.

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

            End If
 
Old September 9th, 2003, 12:36 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Is there a reason you are coding this in a classic ASP style and not utilizing the power of the .Net events? I would highly recommend breaking up the logic and putting it in event handlers for the appropriate control(s). It may clear up some of the problems you are having and will certainly make it easy to read and work with.

Peter
 
Old September 9th, 2003, 12:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

silly question... what's an event? (I just started ASP.NET 2 weeks ago so bear with me)
 
Old September 9th, 2003, 02:03 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
 
Old September 9th, 2003, 02:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Thank you for your help.

you are right the item is NOT selected.

I tried this:
Sub myChange(ByVal s As Object, ByVal e As System.EventArgs) Handles lstEmployees.SelectedIndexChanged
        Dim datHireDate As Date

        If Not lstEmployees.SelectedItem Is Nothing 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
        Else
            Response.Redirect("http://www.google.com")
        End If
End Sub

Do I need this bit of code?
Handles lstEmployees.SelectedIndexChanged
at the end of my sub?
 
Old September 9th, 2003, 03:59 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

In your case: No.

What this is for is when you are using code-behind. In a code-behind file the variables that you declare that match up to controls on the page are declared like this:
Protected WithEvents myDropDownList As DropDownList
This gives you the ability to tie the controls events to the methods by means of the "Handles" keyword. You then don't have to specify the method name in the control (OnSelectedIndexChanged="myChangeMethod").

When you are working with inline code however, you can't do this because you can not declare the control in the page's code section. So you have to specify the target event handler in the control's HTML itself.

Of course, the confusing part of all this is that "Handles MyBase.Load" bit. This is a different case because MyBase is an object instance that is accessible from the page. It's the only way you can write handlers for base class events.

I understand how confusing this is, it took me a long time to wrap my head around the differences between ASP w/ VBScript and ASP.Net with VB.Net. It's especially harder when you are trying to learn .Net without the aid of Visual Studio. I have learned a lot just from having that environment to work in.

Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
AJAX and AutoPostBack [email protected] Ajax 0 May 9th, 2006 07:34 AM
AutoPostBack problem liorlankri ASP.NET 1.x and 2.0 Application Design 1 December 22nd, 2004 09:45 AM
Repeater and DropDownList with AutoPostBack mdonstad General .NET 1 October 5th, 2004 11:38 AM
Listbox w/o Autopostback collie ASP.NET 1.0 and 1.1 Basics 3 November 21st, 2003 01:57 PM
linkbutton and autopostback jtyson ASP.NET 1.0 and 1.1 Basics 0 July 10th, 2003 11:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.