Question DropDownList SelectedIndexChanged problem?

raysefo

Well-known member
Joined
Jul 21, 2010
Messages
207
Programming Experience
Beginner
Hi,

SelectedIndexChanged does NOT fired. Here is the code;
Any ideas?

Thanks in advance.

Best Regards

aspx
VB.NET:
....
 <asp:Panel runat="server" ID="Panel3">
            <table class="table">
                <col class="table_left" />
                <col class="table_right" />
                
                <tr>
                    <td>
                        Bank<span class="red">*</span>
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList_bank" runat="server">
                        </asp:DropDownList>
                    </td>
                </tr>
.....

aspx.vb
VB.NET:
....
 If Not Page.IsPostBack Then
           
            If no= 5 Then

                vendorNo = Convert.ToInt32(DetailsView1.Rows(0).Cells(1).Text)
                
                Dim conn5 As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
                Try
                    Dim comm5 As New Data.SqlClient.SqlCommand("SELECT Bank,No,IBAN FROM Accounts WHERE name like '%" + vendorNo.ToString + "%'", conn5)
                    Dim SQLSelectAdapter5 As New SqlDataAdapter()

                    SQLSelectAdapter5.SelectCommand = comm5
                    conn5.Open()

                    Dim dt5 As New DataTable
                    SQLSelectAdapter5.Fill(dt5)
                    If dt5.Rows.Count > 0 Then
                        'SQLSelectAdapter5.Dispose()
                        DropDownList_bank.DataSource = dt5
                        DropDownList_bank.DataTextField = "Bank"
                        'DropDownList_bank.DataValueField = "Bank"
                        DropDownList_bank.DataBind()
                        
                    End If

                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                    loggerDB.Error(ex.Message, ex)
                Finally
                    'Close the connection
                    conn5.Close()
                End Try

SelectedIndexChanged
VB.NET:
 Protected Sub DropDownList_bank_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList_bank.SelectedIndexChanged
Dim conn5 As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Try
            Dim comm5 As New Data.SqlClient.SqlCommand("SELECT Bank,No,IBAN FROM Accounts WHERE name like '%" + Me.ViewState("vendorNo").ToString + "%' and Bank = '" + DropDownList_bank.SelectedItem.Value+"'", conn5)
            Dim SQLSelectAdapter5 As New SqlDataAdapter()

            SQLSelectAdapter5.SelectCommand = comm5
            conn5.Open()

            Dim dt5 As New DataTable
            SQLSelectAdapter5.Fill(dt5)
            If dt5.Rows.Count > 0 Then
                SQLSelectAdapter5.Dispose()
                DropDownList_bank.DataSource = dt5
                DropDownList_bank.DataTextField = "Bank"
                DropDownList_bank.DataValueField = "Bank"
                DropDownList_bank.DataBind()
                
            End If

        Catch ex As Exception
            Console.WriteLine(ex.Message)
            loggerDB.Error(ex.Message, ex)
        Finally
            'Close the connection
            conn5.Close()
        End Try
....
 
You need to tell it to post back to the server:
VB.NET:
<asp:DropDownList ID="DropDownList_bank" runat="server" [B]AutoPostBack="True"[/B]>
</asp:DropDownList>
Then the SelectedIndexChanged event will fire on the server
 
And perferably set this property in Design view; select the control and modify the property in Properties window. The markup is then generated. In a visual development environment like Visual Studio it is important to emphasize and utilize the visual aspect of development, using Designers is a good thing :)

AutoPostBack is False by default for this control, to allow the user to possibly make multiple selections and input before invoking server postback, for example with 'submit' button. The SelectedIndexChanged event handler is also then called (if selection has changed).
 

Latest posts

Back
Top