SelectedIndexChanged in DropDownList won't trigger

Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Jan 2006
Posts: 14
Reputation: Texpert is an unknown quantity at this point 
Solved Threads: 0
Texpert's Avatar
Texpert Texpert is offline Offline
Newbie Poster

SelectedIndexChanged in DropDownList won't trigger

 
0
  #1
Mar 16th, 2006
Hi,

I have two drop down lists boxes, upon page load first DDL gets populated and depending on user's selection on first DDL second DDL (child DDL) gets populated, works fine. My problem is when user goes back and makes another selection on first (parent) DDL (note that it's not a postback event, since user did not leave the page yet!! OR is that right? I am bit confused about the IsPostBack property),
So when selection in first DDL changes, it should automatically clear the Child DDL (which is still showing values for the first selection, which is deceiving)
When user actually clicks on a button to populate the Child DDL, it shows correct values for the new selection.
But when I use SelectedIndexChanged even on first DDL to clear Child DDL, it never gets triggered
This is my code
Any help is highly appreciated

  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. 'Put user code to initialize the page here
  3. If Not IsPostBack() Then
  4.  
  5.  
  6. 'Session("Current_Release_Id") = 1
  7.  
  8. Dim MySQLString As String
  9. Dim objParam1 As SqlParameter
  10. Dim MyReader As SqlDataReader
  11. Dim MyConn As SqlConnection
  12. Dim Mycmd As SqlCommand
  13. Dim MyDataAdapter As SqlDataAdapter
  14.  
  15. Dim cnt As Integer
  16. Dim str As String
  17. Dim MyDataSet As New DataSet
  18.  
  19.  
  20. Try
  21.  
  22. MyConn = New SqlConnection(ConfigurationSettings.AppSettings("Conn"))
  23.  
  24. MySQLString = "Select DISTINCT PackageName , PackageId "
  25. MySQLString = MySQLString + " FROM Package "
  26. MySQLString = MySQLString + "WHERE (Package.ReleaseId = @PackageRelId) ORDER BY PackageName"
  27.  
  28. MyDataAdapter = New SqlDataAdapter(MySQLString, MyConn)
  29.  
  30. objParam1 = MyDataAdapter.SelectCommand.Parameters.Add("@PackageRelId", SqlDbType.Int)
  31. objParam1.Value = Rel_Matrix_Id.Text.Trim
  32. str = Rel_Matrix_Id.Text.Trim
  33.  
  34. PackageNameDDL.Items.Clear()
  35. If MyConn.State = ConnectionState.Closed Then
  36. MyConn.Open()
  37. End If
  38.  
  39.  
  40. cnt = MyDataAdapter.Fill(MyDataSet)
  41.  
  42. If cnt = 0 Then
  43. ComponentDDL.Visible = False
  44. CompBtn.Visible = False
  45. Say("No Packages are Added to this Release!!")
  46. Exit Try
  47. End If
  48.  
  49. ComponentDDL.Visible = True
  50. CompBtn.Visible = True
  51.  
  52.  
  53. 'MyDataAdapter.Dispose()
  54.  
  55. PackageNameDDL.DataSource = MyDataSet
  56. PackageNameDDL.DataTextField = "PackageName"
  57. PackageNameDDL.DataValueField = "PackageId"
  58. PackageNameDDL.DataBind()
  59.  
  60. MyConn.Dispose()
  61. MyConn.Close()
  62. Catch ex As Exception
  63. Say("[PageLoad] Exception " & ex.Message)
  64. System.Diagnostics.Trace.WriteLine("[ShowBtnClick] Exception " & ex.Message)
  65. Finally
  66. MyConn.Close()
  67. End Try
  68.  
  69. End If
  70. End Sub

First DDL is populated and now after click of a button second DDL gets populated
  1. Private Sub CompBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompBtn.Click
  2. Dim MySQLString As String
  3.  
  4. Dim objParam1 As SqlParameter
  5. Dim MyReader As SqlDataReader
  6. Dim MyConn As SqlConnection
  7. Dim Mycmd As SqlCommand
  8. Dim MyDataAdapter As SqlDataAdapter
  9.  
  10. Dim cnt As Integer
  11. Dim str As String
  12. Dim MyDataSet As New DataSet
  13.  
  14.  
  15. Try
  16.  
  17. MyConn = New SqlConnection(ConfigurationSettings.AppSettings("Conn"))
  18.  
  19. MySQLString = "Select DISTINCT ComponentID , Description "
  20. MySQLString += " FROM Component"
  21. MySQLString += " WHERE (PackageID = @PackageId) ORDER BY Description"
  22.  
  23. MyDataAdapter = New SqlDataAdapter(MySQLString, MyConn)
  24.  
  25. objParam1 = MyDataAdapter.SelectCommand.Parameters.Add("@PackageId", SqlDbType.Int)
  26. objParam1.Value = PackageNameDDL.SelectedItem.Value.Trim
  27.  
  28. ComponentDDL.Items.Clear()
  29. If MyConn.State = ConnectionState.Closed Then
  30. MyConn.Open()
  31. End If
  32.  
  33.  
  34. cnt = MyDataAdapter.Fill(MyDataSet)
  35.  
  36. If cnt = 0 Then
  37. Say("No Components are Added to this Package!!")
  38. Exit Try
  39. End If
  40.  
  41. 'MyDataAdapter.Dispose()
  42.  
  43. ComponentDDL.DataSource = MyDataSet
  44. ComponentDDL.DataTextField = "Description"
  45. ComponentDDL.DataValueField = "ComponentID"
  46. ComponentDDL.DataBind()
  47.  
  48.  
  49.  
  50. MyConn.Dispose()
  51. MyConn.Close()
  52. Catch ex As Exception
  53. Say("[ShowComponent BtnClick] Exception " & ex.Message)
  54. System.Diagnostics.Trace.WriteLine("[ShowComponent BtnClick] Exception " & ex.Message)
  55. Finally
  56. MyConn.Close()
  57. End Try
  58. End Sub

works great... but what if user wants to change the package (first DDL) selection, my child DDL still shows old values until user clicks on the button again.. I tried to do something like this
  1. Private Sub PackageNameDDL_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PackageNameDDL.SelectedIndexChanged
  2. ComponentDDL.Items.Clear()
  3. End Sub
but the event never triggers, what am I doing wrong ?
:cry:
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1
Reputation: than is an unknown quantity at this point 
Solved Threads: 0
than than is offline Offline
Newbie Poster

Re: SelectedIndexChanged in DropDownList won't trigger

 
0
  #2
Jun 28th, 2007
I have an very problem very similar to yours. Any helps from anyone? Thanks.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 65
Reputation: SolTec is an unknown quantity at this point 
Solved Threads: 4
SolTec SolTec is offline Offline
Junior Poster in Training

Re: SelectedIndexChanged in DropDownList won't trigger

 
0
  #3
Feb 21st, 2008
Originally Posted by Texpert View Post
Hi,

I have two drop down lists boxes, upon page load first DDL gets populated and depending on user's selection on first DDL second DDL (child DDL) gets populated, works fine. My problem is when user goes back and makes another selection on first (parent) DDL (note that it's not a postback event, since user did not leave the page yet!! OR is that right? I am bit confused about the IsPostBack property),
So when selection in first DDL changes, it should automatically clear the Child DDL (which is still showing values for the first selection, which is deceiving)
When user actually clicks on a button to populate the Child DDL, it shows correct values for the new selection.
But when I use SelectedIndexChanged even on first DDL to clear Child DDL, it never gets triggered
This is my code
Any help is highly appreciated

  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. 'Put user code to initialize the page here
  3. If Not IsPostBack() Then
  4.  
  5.  
  6. 'Session("Current_Release_Id") = 1
  7.  
  8. Dim MySQLString As String
  9. Dim objParam1 As SqlParameter
  10. Dim MyReader As SqlDataReader
  11. Dim MyConn As SqlConnection
  12. Dim Mycmd As SqlCommand
  13. Dim MyDataAdapter As SqlDataAdapter
  14.  
  15. Dim cnt As Integer
  16. Dim str As String
  17. Dim MyDataSet As New DataSet
  18.  
  19.  
  20. Try
  21.  
  22. MyConn = New SqlConnection(ConfigurationSettings.AppSettings("Conn"))
  23.  
  24. MySQLString = "Select DISTINCT PackageName , PackageId "
  25. MySQLString = MySQLString + " FROM Package "
  26. MySQLString = MySQLString + "WHERE (Package.ReleaseId = @PackageRelId) ORDER BY PackageName"
  27.  
  28. MyDataAdapter = New SqlDataAdapter(MySQLString, MyConn)
  29.  
  30. objParam1 = MyDataAdapter.SelectCommand.Parameters.Add("@PackageRelId", SqlDbType.Int)
  31. objParam1.Value = Rel_Matrix_Id.Text.Trim
  32. str = Rel_Matrix_Id.Text.Trim
  33.  
  34. PackageNameDDL.Items.Clear()
  35. If MyConn.State = ConnectionState.Closed Then
  36. MyConn.Open()
  37. End If
  38.  
  39.  
  40. cnt = MyDataAdapter.Fill(MyDataSet)
  41.  
  42. If cnt = 0 Then
  43. ComponentDDL.Visible = False
  44. CompBtn.Visible = False
  45. Say("No Packages are Added to this Release!!")
  46. Exit Try
  47. End If
  48.  
  49. ComponentDDL.Visible = True
  50. CompBtn.Visible = True
  51.  
  52.  
  53. 'MyDataAdapter.Dispose()
  54.  
  55. PackageNameDDL.DataSource = MyDataSet
  56. PackageNameDDL.DataTextField = "PackageName"
  57. PackageNameDDL.DataValueField = "PackageId"
  58. PackageNameDDL.DataBind()
  59.  
  60. MyConn.Dispose()
  61. MyConn.Close()
  62. Catch ex As Exception
  63. Say("[PageLoad] Exception " & ex.Message)
  64. System.Diagnostics.Trace.WriteLine("[ShowBtnClick] Exception " & ex.Message)
  65. Finally
  66. MyConn.Close()
  67. End Try
  68.  
  69. End If
  70. End Sub

First DDL is populated and now after click of a button second DDL gets populated
  1. Private Sub CompBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompBtn.Click
  2. Dim MySQLString As String
  3.  
  4. Dim objParam1 As SqlParameter
  5. Dim MyReader As SqlDataReader
  6. Dim MyConn As SqlConnection
  7. Dim Mycmd As SqlCommand
  8. Dim MyDataAdapter As SqlDataAdapter
  9.  
  10. Dim cnt As Integer
  11. Dim str As String
  12. Dim MyDataSet As New DataSet
  13.  
  14.  
  15. Try
  16.  
  17. MyConn = New SqlConnection(ConfigurationSettings.AppSettings("Conn"))
  18.  
  19. MySQLString = "Select DISTINCT ComponentID , Description "
  20. MySQLString += " FROM Component"
  21. MySQLString += " WHERE (PackageID = @PackageId) ORDER BY Description"
  22.  
  23. MyDataAdapter = New SqlDataAdapter(MySQLString, MyConn)
  24.  
  25. objParam1 = MyDataAdapter.SelectCommand.Parameters.Add("@PackageId", SqlDbType.Int)
  26. objParam1.Value = PackageNameDDL.SelectedItem.Value.Trim
  27.  
  28. ComponentDDL.Items.Clear()
  29. If MyConn.State = ConnectionState.Closed Then
  30. MyConn.Open()
  31. End If
  32.  
  33.  
  34. cnt = MyDataAdapter.Fill(MyDataSet)
  35.  
  36. If cnt = 0 Then
  37. Say("No Components are Added to this Package!!")
  38. Exit Try
  39. End If
  40.  
  41. 'MyDataAdapter.Dispose()
  42.  
  43. ComponentDDL.DataSource = MyDataSet
  44. ComponentDDL.DataTextField = "Description"
  45. ComponentDDL.DataValueField = "ComponentID"
  46. ComponentDDL.DataBind()
  47.  
  48.  
  49.  
  50. MyConn.Dispose()
  51. MyConn.Close()
  52. Catch ex As Exception
  53. Say("[ShowComponent BtnClick] Exception " & ex.Message)
  54. System.Diagnostics.Trace.WriteLine("[ShowComponent BtnClick] Exception " & ex.Message)
  55. Finally
  56. MyConn.Close()
  57. End Try
  58. End Sub

works great... but what if user wants to change the package (first DDL) selection, my child DDL still shows old values until user clicks on the button again.. I tried to do something like this
  1. Private Sub PackageNameDDL_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PackageNameDDL.SelectedIndexChanged
  2. ComponentDDL.Items.Clear()
  3. End Sub
but the event never triggers, what am I doing wrong ?
:cry:

Just for giggles, try and set the SelectedIndex = -1 which should be no item selected when you make a new selection from the parent and then re-run your connection string anytime the parent SelectedIndex changes ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC