Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SelectedIndexChanged event for DropDown

Status
Not open for further replies.

hf28

Programmer
Oct 23, 2003
54
0
0
US
Hello everyone,
I'm facing an unexpected problem:

I have a few DropDowns on my WebPage. I'm supposed to populate there values depending on the changes in the previous ones. For example, I have cboSchools.

On the Page_load event, I populate the cboSchool. It works. Then on cboSchool_SelectedIndexChanged I'll have to populate the DropDown cboClassroom, but first I want to see what value from the cboSchool the system reads.

I say

Private Sub cboSchool_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSchool.SelectedIndexChanged
Response.Write (cboSchool.ItemsddlSchool.SelectedIndex).Value)
End sub

Somehow, I don't even get to this function.

Does anyone knows what I'm doing wrong.

Thanks a lot in advance.
 
hf:

Whenever I need a combobox (dropdownlist) value I usually use the following:

...ddGroups.SelectedItem.Value...

This works just fine in all cases, i.e., it simply gets the value of the ddGroups dropdown. In case of multiple selections, of course you would have to modify with a loop through process.
 
Thanks, Isadore.
I don't think it'll work in my case. See I already populated the first DD. The second one is just empty for now. The second one will be populated only if the Index_changed event happened in the first DD. It's a prebuilt Sub, but somehow, when I change the Index in the first DD, I don't get to the function.
 
hf,

I did the same thing you are trying to do, and it works as it should. I would suggest checking the declaration in the dropdownlist for the event OnSelectedIndexChanged. In my Sub, I took the value of the Item and had it write to an asp label.


Good Luck,

Rob
 
hf,

Also, if you want it to fire when the change occurs, make sure you have AutoPostBack set to True.


Rob
 
Do you have the AutoPostBack set to True?

Hope everyone is having a great day!

Thanks - Jennifer
 
Thanks, guys. That's exctly what I'm doing:

1. Created a Generic function:

'Function to populate the specified dropdown.
Public Function FillDropDownList(ByVal obj As DropDownList, ByVal sqlstr As String, ByVal bSelect As Boolean, ByVal strCacheName As String)

'First clear the data in the dropdown.
obj.Items.Clear()

'First check if this requested information needs to be cached (i.e. it won't change)
'Check if the data exists in Cache. If it does, then use it, otherwise pull it from Cache
If strCacheName = "" Or Cache.Item(strCacheName) Is Nothing Then
Dim CmdDropDown As SqlCommand
' Dim dtrDropDown As SqlDataReader
Dim InitRow As Boolean

InitRow = True

'CmdDropDown = New SqlCommand(sqlstr, Conn)
'dtrDropDown = CmdDropDown.ExecuteReader()

Dim dtrDropDown As SqlDataReader = SqlHelper.ExecuteReader(Global.Project.ConnectionString, CommandType.Text, sqlstr)


While dtrDropDown.Read()
If InitRow And bSelect Then
obj.Items.Add(New ListItem("---Please select---", "0"))
InitRow = False
End If

obj.Items.Add(New ListItem(dtrDropDown(0), dtrDropDown(1)))
End While

dtrDropDown.Close()

'Put it in cache to expire in a day.
Cache.Insert(strCacheName, obj, Nothing, DateTime.Today.AddDays(1), Nothing)
Else
Dim CacheObj As DropDownList
CacheObj = Cache(strCacheName)

Dim Counter As Integer, lText As String, lValue As String

For Counter = 0 To CacheObj.Items.Count - 1
lText = CacheObj.Items(Counter).Text
lValue = CacheObj.Items(Counter).Value
obj.Items.Add(New ListItem(lText, lValue))
Next
End If

End Function

2. 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 sqlstr As String
SiteId.Text = ConfigurationSettings.AppSettings("SiteID")

'Dim sname As String
Dim sID As Integer

'Dim l_Schools As New l_Schools(Global.Project, Global.CurrentSite.SiteID.Value)

If Not Page.IsPostBack Then

'Fill schools
sqlstr = "Select Name, SchoolID from tlkpSchool where SiteID=" & Global.CurrentSite.SiteID.Value
FillDropDownList(ddlSchool, sqlstr, True, "SACD_ddlSchool")

End if
End Sub

3.
Private Sub ddlSchool_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlSchool.SelectedIndexChanged

Dim sqlstr As String

sqlstr = "Select ClassRoomID from tlkpCLASSROOM where SchoolID='" & ddlSchool.Items(ddlSchool.SelectedIndex).Value & "'"
FillDropDownList(ddlClassroom, sqlstr, True, "")

End Sub

/////////////////////////////////////

I don't even get into the Sub ddlSchool_SelectedIndexChanged

Thanks again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top