944,156 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 17147
  • VB.NET RSS
Mar 16th, 2006
0

SelectedIndexChanged in DropDownList won't trigger

Expand 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

VB.NET Syntax (Toggle Plain Text)
  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
VB.NET Syntax (Toggle Plain Text)
  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
VB.NET Syntax (Toggle Plain Text)
  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:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Texpert is offline Offline
14 posts
since Jan 2006
Jun 28th, 2007
0

Re: SelectedIndexChanged in DropDownList won't trigger

I have an very problem very similar to yours. Any helps from anyone? Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
than is offline Offline
1 posts
since Jun 2007
Feb 21st, 2008
0

Re: SelectedIndexChanged in DropDownList won't trigger

Click to Expand / Collapse  Quote originally posted by Texpert ...
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

VB.NET Syntax (Toggle Plain Text)
  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
VB.NET Syntax (Toggle Plain Text)
  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
VB.NET Syntax (Toggle Plain Text)
  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 ?
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
SolTec is offline Offline
65 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: can't binding an autoincrement field of mysql data base from vb.net
Next Thread in VB.NET Forum Timeline: Question involving text boxes...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC