Solved

Using WebControls.DropDownList, my SelectedIndexChanged handler never fires, though other handlers do fire

Posted on 2006-07-01
8
1,261 Views
Last Modified: 2012-05-05
In the following code (I include the .ASPX followed by the code-behind ASPX.VB), it can be seen that I set up the handler for OnSelectedIndexChanged for listENV, but no matter what item I choose on the list, listENV_IndexChange() is never executed, though txtDatabaseNum_TextChanged(), btnCancel_Click(), and btnSave_Click() are all executed when their respective events are fired (also, when I write AddHandler and use Intellisense to tell me what objects have events that might have handlers added for them, listEnv does not appear in the list, though all the text controls do):

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="DB_Status.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body bgColor="whitesmoke" MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:button id="btnCancelTop" style="Z-INDEX: 108; LEFT: 24px; POSITION: absolute; TOP: 16px"
                        OnClick="btnCancel_Click" runat="server" Width="168px" Height="25px" Text="Cancel and Return to Menu"
                        CausesValidation="True"></asp:button>
                  <asp:button id="btnSaveTop" style="Z-INDEX: 119; LEFT: 208px; POSITION: absolute; TOP: 16px"
                        OnClick="btnSave_Click" runat="server" Width="160px" Height="25px" Text="Save and Return to Menu"
                        CausesValidation="True"></asp:button>
                  <asp:button id="btnCancelBottom" style="Z-INDEX: 106; LEFT: 24px; POSITION: absolute; TOP: 480px"
                        OnClick="btnCancel_Click" runat="server" Width="168px" Height="25px" Text="Cancel and Return to Menu"></asp:button>
                  <asp:button id="btnSaveBottom" style="Z-INDEX: 107; LEFT: 208px; POSITION: absolute; TOP: 480px"
                        OnClick="btnSave_Click" runat="server" Width="160px" Height="25px" Text="Save and Return to Menu"></asp:button>
                  <asp:label id="lblENV" style="Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 88px" runat="server"
                        Width="152px" Height="8px" Font-Bold="True" ForeColor="#0033FF">Environment:</asp:label>
                  <asp:dropdownlist
                        id="listENV"
                        style="Z-INDEX: 101; LEFT: 192px; POSITION: absolute; TOP: 88px"
                        runat="server"
                        OnSelectedIndexChanged="listENV_IndexChange"
                        AutoPostBack="True"
                        Width="108px"
                        Height="40px"
                        Enabled="True"
                        EnableViewState="True"
                        Visible="True"></asp:dropdownlist>
                  <asp:requiredfieldvalidator id="reqENV" style="Z-INDEX: 115; LEFT: 344px; POSITION: absolute; TOP: 88px" runat="server"
                        Width="16px" Height="8px" ErrorMessage="RequiredFieldValidator" ControlToValidate="listEnv"></asp:requiredfieldvalidator>
                  <asp:label id="lblDatabaseNum" style="Z-INDEX: 104; LEFT: 25px; POSITION: absolute; TOP: 120px"
                        runat="server" Width="152px" Height="10px" Font-Bold="True" ForeColor="#0033FF">Database #:</asp:label>
                  <asp:textbox id="txtDatabaseNum" style="Z-INDEX: 102; LEFT: 192px; POSITION: absolute; TOP: 120px"
                        OnTextChanged="txtDatabaseNum_TextChanged" runat="server" Width="105px" Height="21px" AutoPostBack="True"></asp:textbox>
                  <asp:requiredfieldvalidator id="reqDatabaseNum" style="Z-INDEX: 114; LEFT: 344px; POSITION: absolute; TOP: 120px"
                        runat="server" Width="16px" Height="8px" ErrorMessage="RequiredFieldValidator" ControlToValidate="txtDatabaseNum"></asp:requiredfieldvalidator>
                  <asp:label id="lblSoftwareTree" style="Z-INDEX: 105; LEFT: 24px; POSITION: absolute; TOP: 155px"
                        runat="server" Width="152px" Height="10px" Font-Bold="True" ForeColor="#0033FF">Sofware Tree:</asp:label>
                  <asp:textbox id="txtSoftwareTree" style="Z-INDEX: 109; LEFT: 192px; POSITION: absolute; TOP: 152px"
                        runat="server" Width="105px" Height="22px" MaxLength="3" AutoPostBack="True"></asp:textbox>
                  <asp:requiredfieldvalidator id="reqSoftwareTree" style="Z-INDEX: 116; LEFT: 344px; POSITION: absolute; TOP: 155px"
                        runat="server" Width="8px" Height="8px" ErrorMessage="RequiredFieldValidator" ControlToValidate="txtSoftwareTree"></asp:requiredfieldvalidator>
                  <asp:label id="lblActive" style="Z-INDEX: 112; LEFT: 24px; POSITION: absolute; TOP: 192px"
                        runat="server" Width="152px" Height="8px" Font-Bold="True" ForeColor="Blue">Database is active:</asp:label>
                  <asp:checkbox id="chkActive" style="Z-INDEX: 110; LEFT: 192px; POSITION: absolute; TOP: 192px"
                        runat="server" Width="104px" Height="24px" AutoPostBack="True"></asp:checkbox>
                  <asp:table id="tblPrimeTimeEntry" style="Z-INDEX: 111; LEFT: 24px; POSITION: absolute; TOP: 288px"
                        runat="server" Width="50%" BorderColor="Black" BorderStyle="Solid">
                        <asp:TableRow>
                              <asp:TableCell>
                              Monday thru Friday
                              </asp:TableCell>
                              <asp:TableCell>
                                    <input name="hh_mon_fri_start" type="text" value="07" maxlength="2" id="hh_mon_fri_start"
                                          runat="server" style="color:Black;background-color:Yellow;width:40%;" />
                                    :
                                    <input name="mm_mon_fri_start" type="text" value="00" maxlength="2" id="mm_mon_fri_start"
                                          runat="server" style="color:Black;background-color:Yellow;width:40%;" />
                              </asp:TableCell>
                              <asp:TableCell Width="5%">
                              &nbsp;
                              </asp:TableCell>
                              <asp:TableCell>
                                    <input name="hh_mon_fri_end" type="text" value="19" maxlength="2" id="hh_mon_fri_end" runat="server"
                                          style="color:Black;background-color:Yellow;width:40%;" />
                                    :
                                    <input name="mm_mon_fri_end" type="text" value="00" maxlength="2" id="mm_mon_fri_end" runat="server"
                                          style="color:Black;background-color:Yellow;width:40%;" />
                              </asp:TableCell>
                        </asp:TableRow>
                        <asp:TableRow>
                              <asp:TableCell>
                              Saturday
                              </asp:TableCell>
                              <asp:TableCell>
                                    <input name="hh_sat_start" type="text" value="00" maxlength="2" id="hh_sat_start" runat="server"
                                          style="color:Black;background-color:Yellow;width:40%;" />
                                    :
                                    <input name="mm_sat_start" type="text" value="00" maxlength="2" id="mm_sat_start" runat="server"
                                          style="color:Black;background-color:Yellow;width:40%;" />
                              </asp:TableCell>
                              <asp:TableCell Width="5%">
                              &nbsp;
                              </asp:TableCell>
                              <asp:TableCell>
                                    <input name="hh_sat_end" type="text" value="00" maxlength="2" id="hh_sat_end" runat="server"
                                          style="color:Black;background-color:Yellow;width:40%;" />
                                    :
                                    <input name="mm_sat_end" type="text" value="00" maxlength="2" id="mm_sat_end" runat="server"
                                          style="color:Black;background-color:Yellow;width:40%;" />
                              </asp:TableCell>
                        </asp:TableRow>
                        <asp:TableRow>
                              <asp:TableCell>
                              Sunday
                              </asp:TableCell>
                              <asp:TableCell>
                                    <input name="hh_sun_start" type="text" value="00" maxlength="2" id="hh_sun_start" style="color:Black;background-color:Yellow;width:40%;" />
                                    :
                                    <input name="mm_sun_start" type="text" value="00" maxlength="2" id="mm_sun_start" style="color:Black;background-color:Yellow;width:40%;" />
                              </asp:TableCell>
                              <asp:TableCell Width="5%">
                              &nbsp;
                              </asp:TableCell>
                              <asp:TableCell>
                                    <input name="hh_sun_end" type="text" value="00" maxlength="2" id="hh_sun_end" style="color:Black;background-color:Yellow;width:40%;" />
                                    :
                                    <input name="mm_sun_end" type="text" value="00" maxlength="2" id="mm_sun_end" style="color:Black;background-color:Yellow;width:40%;" />
                              </asp:TableCell>
                        </asp:TableRow>
                  </asp:table>
                  <asp:validationsummary id="vsAddDB" style="Z-INDEX: 113; LEFT: 384px; POSITION: absolute; TOP: 64px" runat="server"
                        Width="256px" Height="264px"></asp:validationsummary><asp:label id="lblPermittedNodes" style="Z-INDEX: 117; LEFT: 24px; POSITION: absolute; TOP: 232px"
                        runat="server" Width="152px" Height="8px" Font-Bold="True" ForeColor="Blue" Enabled="False">Permitted Nodes for Execution:</asp:label><asp:dropdownlist id="dlstPermittedNodes" style="Z-INDEX: 118; LEFT: 192px; POSITION: absolute; TOP: 232px"
                        runat="server" Width="169px" Height="16px" AutoPostBack="True" Enabled="False"></asp:dropdownlist>
            </form>
      </body>
</HTML>

Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected hh_mon_fri_start As System.Web.UI.HtmlControls.HtmlInputText
    Protected mm_mon_fri_start As System.Web.UI.HtmlControls.HtmlInputText
    Protected hh_mon_fri_end As System.Web.UI.HtmlControls.HtmlInputText
    Protected mm_mon_fri_end As System.Web.UI.HtmlControls.HtmlInputText
    Protected hh_sat_start As System.Web.UI.HtmlControls.HtmlInputText
    Protected mm_sat_start As System.Web.UI.HtmlControls.HtmlInputText
    Protected hh_sat_end As System.Web.UI.HtmlControls.HtmlInputText
    Protected mm_sat_end As System.Web.UI.HtmlControls.HtmlInputText
    Protected hh_sun_start As System.Web.UI.HtmlControls.HtmlInputText
    Protected mm_sun_start As System.Web.UI.HtmlControls.HtmlInputText
    Protected hh_sun_end As System.Web.UI.HtmlControls.HtmlInputText
    Protected mm_sun_end As System.Web.UI.HtmlControls.HtmlInputText
    Protected WithEvents btnCancelTop As System.Web.UI.WebControls.Button
    Protected WithEvents btnSaveTop As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancelBottom As System.Web.UI.WebControls.Button
    Protected WithEvents btnSaveBottom As System.Web.UI.WebControls.Button
    Protected WithEvents lblENV As System.Web.UI.WebControls.Label
    Protected WithEvents listENV As System.Web.UI.WebControls.DropDownList
    Protected WithEvents reqENV As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents lblDatabaseNum As System.Web.UI.WebControls.Label
    Protected WithEvents txtDatabaseNum As System.Web.UI.WebControls.TextBox
    Protected WithEvents reqDatabaseNum As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents lblSoftwareTree As System.Web.UI.WebControls.Label
    Protected WithEvents txtSoftwareTree As System.Web.UI.WebControls.TextBox
    Protected WithEvents reqSoftwareTree As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents lblActive As System.Web.UI.WebControls.Label
    Protected WithEvents chkActive As System.Web.UI.WebControls.CheckBox
    Protected WithEvents tblPrimeTimeEntry As System.Web.UI.WebControls.Table
    Protected WithEvents vsAddDB As System.Web.UI.WebControls.ValidationSummary
    Protected WithEvents lblPermittedNodes As System.Web.UI.WebControls.Label
    Protected WithEvents dlstPermittedNodes As System.Web.UI.WebControls.DropDownList

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private m_adb As AvailabilityDBaccess

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim i As Integer
        Dim evl As System.Diagnostics.EventLog
        Dim hshArray() As Hashtable


        If Not IsPostBack Then
            Response.Cache.SetExpires(DateTime.Now)
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
            Response.Cache.SetValidUntilExpires(True)

            evl = New System.Diagnostics.EventLog
            If Not evl.SourceExists("SBODisplayStatus") Then
                evl.CreateEventSource("SBODisplayStatus", "Application")
            End If
            evl.Source = "SBODisplayStatus"
            m_adb = New AvailabilityDBaccess(evl, False, Request.PhysicalApplicationPath)

            Session("adb") = m_adb
            hshArray = m_adb.getAllEnvironments()
            listENV.Items.Clear()
            listENV.Items.Add("")
            listENV.Items(0).Value = 0

            For i = 0 To hshArray.Length - 1
                listENV.Items.Add(hshArray(i)("env_name"))
                listENV.Items(i).Value = CType(hshArray(i)("env_id"), Int32)
            Next

        Else
            'm_adb = Session("adb")
        End If
    End Sub

    Protected Sub listENV_IndexChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listENV.SelectedIndexChanged

        ViewState("listENVSelectedIndex") = listENV.SelectedIndex
        If listENV.SelectedIndex < 1 Then
            ViewState("env_name") = ""
        Else
            ViewState("env_name") = listENV.SelectedItem.Text
        End If

    End Sub

    Protected Sub txtDatabaseNum_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDatabaseNum.TextChanged
        Dim env_name As String
        Dim database_num As String
        Dim hsh As Hashtable

        listENV.SelectedIndex = ViewState("listENVSelectedIndex")
        env_name = ViewState("env_name")
        database_num = Trim(txtDatabaseNum.Text)

        hsh = m_adb.getDatabaseSetupByEnvNameDBNum(env_name, database_num)

        chkActive.Checked = hsh("active") <> 0
        hh_mon_fri_start.Value = Format(CDate(hsh("hh_mon_fri_start")), "HH")
        hh_mon_fri_end.Value = Format(CDate(hsh("hh_mon_fri_end")), "HH")
        hh_sat_start.Value = Format(CDate(hsh("hh_sat_start")), "HH")
        hh_sat_end.Value = Format(CDate(hsh("hh_sat_end")), "HH")
        hh_sun_start.Value = Format(CDate(hsh("hh_sun_start")), "HH")
        hh_sun_end.Value = Format(CDate(hsh("hh_sun_end")), "HH")
        mm_mon_fri_start.Value = Format(CDate(hsh("mm_mon_fri_start")), "mm")
        mm_mon_fri_end.Value = Format(CDate(hsh("mm_mon_fri_end")), "mm")
        mm_sat_start.Value = Format(CDate(hsh("mm_sat_start")), "mm")
        mm_sat_end.Value = Format(CDate(hsh("mm_sat_end")), "mm")
        mm_sun_start.Value = Format(CDate(hsh("mm_sun_start")), "mm")
        mm_sun_end.Value = Format(CDate(hsh("mm_sun_end")), "mm")
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Response.Redirect("supervisor_menu.aspx")
    End Sub

    Protected Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Response.Redirect("supervisor_menu.aspx")
    End Sub



End Class
0
Comment
Question by:Christopher Kile
8 Comments
 
LVL 26

Expert Comment

by:Rejojohny
Comment Utility
i do not think this is needed within ur dropdownlist definition in the aspx page ...
OnSelectedIndexChanged="listENV_IndexChange"

as the event handler has the handles statement ..

But i am confused with the code within the selectedindexchanged event .. y do u need to save the values in viewstate? if its just to get the selected value .. u could use the code
listENV.SelectedValue ....
0
 
LVL 12

Expert Comment

by:deanvanrooyen
Comment Utility
hi,

i used your aspx droplist only as i use c#, it posted back ok...

does the page post back at all when you change selected item? tip - put a break point in all your event handlers to see if one of the other event handlers are handling it first...
0
 
LVL 23

Author Comment

by:Christopher Kile
Comment Utility
You're gonna be very surpised (as I was) but the answer turned out to be that the way I was attempting to set the value property of a list item in the code-behind caused the event not to fire.  It was very subtle; the compiler didn't catch it, and the runtime did not throw an error, so I assume this usage has a legitimate purpose of some sort.  One of the offending lines is this:

listENV.Items(i).Value = CType(hshArray(i)("env_id"), Int32)

It looks perfectly correct, but THIS is the correct form:

listENV.Items.Item(i).Value = CType(hshArray(i)("env_id"), Int32)

With the first line in place, my SelectedIndexChanged event would not fire; with the second line in place, it did fire.

I used the following test code (simply change Items.Item(i) to Items(i) and you should duplicate the problem immediately):

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="DB_Status.WebForm2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm2</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:DropDownList id="listENV" style="Z-INDEX: 101; LEFT: 216px; POSITION: absolute; TOP: 64px"
                        runat="server" Width="72px" Height="56px" AutoPostBack="True">
                  </asp:DropDownList>
            </form>
      </body>
</HTML>

Public Class WebForm2
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents listENV As System.Web.UI.WebControls.DropDownList

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If Not IsPostBack Then
            listENV.Items.Clear()
            listENV.Items.Add("1")
            listENV.Items.Item(0).Value = 10
            listENV.Items.Add("2")
            listENV.Items.Item(1).Value = 11
            listENV.Items.Add("3")
            listENV.Items.Item(2).Value = 12
            listENV.Items.Add("4")
            listENV.Items.Item(3).Value = 13
        End If
    End Sub

    Protected Sub listENV_SelectedIndexChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listENV.SelectedIndexChanged

        Response.Write("Fired " & CStr(listENV.SelectedIndex) & ", value is " & listENV.Items.Item(listENV.SelectedIndex).Value)

    End Sub

End Class


Does this mean I get the points for answering my own question? :)

BTW, thanks for the help, guys, being told it worked out of the box in a simple scenario gave me the confidence to apply my experience to the problem (I found it by commenting out lines one by one).

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 12

Expert Comment

by:deanvanrooyen
Comment Utility
i found a very similar issue with changing data in a datarow in c#

using DataRow.ItemArray[0] = "value" didnt work even though the comments said Get or Set!
using DataRow[0] = "value" did work
0
 
LVL 23

Author Comment

by:Christopher Kile
Comment Utility
Indeed, that does seem parallel.  And, in fact, though I initially had a working event handler, after a few code changes even the value assignment I used above (....Items.Item(i).Value) caused the event to fail to fire.  When I have time, I will no doubt post this to the MSDN newsgroups and see if the MS key players have anything to say on the subject.  As ASP.NET does considerable code generation, it's quite possible this is a hole in that generation.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
Comment Utility
Closed, 500 points refunded.
ee_ai_construct
Community Support Moderator
replacement part #xm34
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now