We help IT Professionals succeed at work.

Cascading DropDown not working in or out of UpdatePanel

p_love
p_love asked
on
2,702 Views
Last Modified: 2013-11-07
Hello,

I am trying to do something really simple.  I am trying to capture the SelectedIndexChanged event in one dropdown, use the value as a parameter to fire a stored procedure - and then populate a second drop down with the values.

It doesnt work.  When I step through the code, I see that the DataReader is returning records, and appears to be going through the process of adding items to the drop down..only the original entry from the inital page load is in the drop down in the browser.

I have eveything in an UpdatePanel, but it doesnt work in or out of the UpdatePanel.  I have reverted to the DataReader.Read method as a last resort....because the DataBind method wasnt working either.

Surely this is bread and butter stuff!?

Regards

p_love





    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' First we need to populate the drop down lists
        If Not Page.IsPostBack And Not Me.ScriptManager1.IsInAsyncPostBack Then
            Call Bind_Filters()
        End If

    End Sub




    Private Sub Bind_Filters()
        ' Bind all the filters
        Call Bind_DropDown("SP_DUNDAS_LUT_01", Me.ddSubstance, "Substance", False, "@TA", "Oncology/Infection")
        Call Bind_DropDown("SP_DUNDAS_LUT_02", Me.ddProject, "Project", True, "@SUBSTANCE", "(All)")
        Call Bind_DropDown("SP_DUNDAS_LUT_03", Me.ddActivity, "Activity", True, "@PROJECT", "(All)")
        Call Bind_DropDown("SP_DUNDAS_LUT_04", Me.ddRegion, "Region", False)
        Call Bind_DropDown("SP_DUNDAS_LUT_05", Me.ddSite, "Site", True, "@REGION", "(All)")


    End Sub


    Private Sub Bind_DropDown(ByVal strCommand As String, ByVal dd As DropDownList, ByVal strDataTextField As String, ByVal blnInitialise As Boolean, Optional ByVal strParamName As String = Nothing, Optional ByVal strParam As String = Nothing)

        Dim request As New MISF.MISFDataRequest
        Dim param As New MISF.MISFDataRequest.DataParameter
        Dim factory As MISF.MISFDataAbstractFactory
        Dim ds As MISF.MISFDataSet
        Dim dsFinal As New DataSet
        Dim dr As IDataReader


        If blnInitialise = False Then
            factory = New MISF.MISFSqlFactory

            With request
                .Command = strCommand
                .CommandType = CommandType.StoredProcedure

                If Not (strParam Is Nothing) And Not (strParamName Is Nothing) Then
                    ' We only need to add parameters to the collection if we have any!
                    param.ParamName = strParamName
                    param.ParamValue = strParam

                    .Parameters.Add(param)
                End If

                .Role = MISF.MISFDataRequest.UserRole.Writer

                .Transactional = False

            End With

            ds = factory.ExecuteDataSet(request)

            dsFinal = ds.ReturnedDataSet

            dd.Items.Clear()

            dr = dsFinal.Tables(0).CreateDataReader

            While dr.Read
                dd.Items.Add(dr.Item(0))
            End While



        ElseIf blnInitialise = True Then
            ' This is a first pass initialisation and so we only need to add "(All)"
            dd.Items.Add("(All)")
        End If

        Page.DataBind()

    End Sub





    Protected Sub ddSubstance_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddSubstance.SelectedIndexChanged
        Dim strValue As String

        If ScriptManager1.IsInAsyncPostBack Then
            strValue = ddSubstance.SelectedItem.Value
            If strValue = "(All)" Then
                Call Bind_DropDown("SP_DUNDAS_LUT_02", Me.ddProject, "Project", True)
                Call Bind_DropDown("SP_DUNDAS_LUT_03", Me.ddActivity, "Activity", True)
            Else
                Call Bind_DropDown("SP_DUNDAS_LUT_02", Me.ddProject, "Project", False, "@SUBSTANCE", strValue)
                Call Bind_DropDown("SP_DUNDAS_LUT_03", Me.ddActivity, "Activity", False, "@PROJECT", ddProject.SelectedItem.Value)

            End If
        End If


    End Sub
Comment
Watch Question

Top Expert 2007

Commented:
THere is an ajax component that might be perfect for your requirements: -

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/CascadingDropDown/CascadingDropDown.aspx

Author

Commented:
Hi,

Thanks.  I am aware of this control.  The issue I have is that some dropdowns need to cascade and some don't (some have fixed content and are not part of any hierarchy).

I need to understand why this isnt working as it is.

Regards

p_love
Top Expert 2007

Commented:
ok, the easiest way I have found to impliment UpdatePanel stuff is first to get it working without ajax and standard postbacks, then all you need do is wrap it up with the Update Panel and all is ok.

This would be my first recomendaton.

Author

Commented:
OK,

Thanks.  Out of the UpdatePanel it works fine.  I thought the beaty of the UpdatePanel was you just wrapped your controls around it and off you go.

Any ideas?

p_love
CERTIFIED EXPERT
Top Expert 2007

Commented:
what happens if you take out "Page.Databind()" from your Bind_DropDown function?
I'm wondering if the page-level databind is pulling from viewstate....

Author

Commented:
Hi,

I have tried removing both databinds (do I need to databind if I am iterating with the reader?  I dont think so so I removed it).  No joy.  I have added the dd.DataBind() back in and got rid of the  Page.DataBind(), no joy.

I have just tried adding one of the dropdowns as a specific UpdatePanel trigger...no joy.  

I have tried all the above with a Conditional / Always UpdateMode option for the UpdatePanel...no joy!

Regards

p_love
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2007

Commented:
So in your original post, you said:
>>I have eveything in an UpdatePanel, but it doesnt work in or out of the UpdatePanel.

But at postID:20066304, you said it works when it *is* outside of the UpdatePanel?

And when you first hit the page, Bind_Filters runs correctly?

Author

Commented:
I just created a simple example in a new page and it works Async.  Must be something to do with my code..or with the fact a database access is occurring.

Simple example:

  Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        If DropDownList1.SelectedItem.Value = 1 Then
            DropDownList2.Items.Clear()
            DropDownList2.Items.Add(1)
            DropDownList2.Items.Add(2)
            DropDownList2.Items.Add(3)
            DropDownList2.Items.Add(4)
        Else
            DropDownList2.Items.Clear()
            DropDownList2.Items.Add(10)
            DropDownList2.Items.Add(11)
            DropDownList2.Items.Add(12)
            DropDownList2.Items.Add(13)

        End If

Author

Commented:
Hi,

Yes...I made a mistake in my original post.  It does work outside of the UpdatePanel perfectly.

In the UpdatePanel, I can put a breakpoint on the async postback, and it walks through - adding items (apparently) from the datareader to the combo box....Just nothing is displayed in the Browser.

Regards

p_love

Author

Commented:
...its almost as if the part of the asynchronous postback that returns the updated html isnt happening?

Author

Commented:
DOOOHHHHHHH!

I put some javascript in to trace the events of the PageRequestManager to trace what was happening.  It appears that the second item here is quite a long query and so the ScriptManager was timing out before the data results were returned!!

When I remmed out the second line, all worked as expected.

 Call Bind_DropDown("SP_DUNDAS_LUT_02", Me.ddProject, "Project", False, "@SUBSTANCE", strValue)
Call Bind_DropDown("SP_DUNDAS_LUT_03", Me.ddActivity, "Activity", False, "@PROJECT",

If I had put an update progress panel on, I guess I would have seen it.  

Is there any way to disable controls on an async postback?  What confused me was the fact I could still access all the dropdowns and though the postback had finished?

Regards

p_love
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
point taken

thx
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.