Client callback: head not got round: dependent dropdowns

Hello,

I'm going a bit mad with dependent dropdowns using client callback, in the old asp days i would have created three javascript arrays from a database and used them entirely in javascript. I was excited with clientcallback when i heard about it but....

I am trying to use three dependent dropdowns although the script below only tries to render the fist two.

I hve adapted some c# code, which is trying to render a control but also passes back a stringwriter which i down quite know what to do with, i have tried placeholders but i think my javascript call is causing the callback functions to keep looping and looping and looping.

i want to write out webcontols rather than select boxes if possibel but this isn't essential

any assistance will be verymuch appreciated.

thanks

Mark



Imports System.Configuration

Imports System.Collections

Imports System.Web

Imports System.Web.Security

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Imports System.Web.UI.HtmlControls

'Imports System.Data.SqlClient

Imports System.IO

Imports IAU.Core.BusinessLayer

Partial Public Class cbtest
    Inherits System.Web.UI.Page

    Implements System.Web.UI.ICallbackEventHandler

    Protected argumentValue As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        ' Create the client callbacks

        'ddlCategory.Attributes.Add("onchange", "GetChildren(this.options[this.selectedIndex].value, 'ddl');")
        'Dim callBack As String = Page.ClientScript.GetCallbackEventReference(Me, "arg", "ClientCallback", "context")
        'Dim clientFunction As String = "function GetChildren(arg, context){ " + callBack + "; }"
        'Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "GetChildren", clientFunction, True)

        Dim cbReference As String = ClientScript.GetCallbackEventReference(Me, "arg", "CallServer", "context")

        Dim script As String = "function CallServer(arg,context) {" + cbReference + "}"

        ClientScript.RegisterClientScriptBlock(Me.GetType(), "CallServer", script, True)



        If Not Page.IsPostBack Then

            getFosTables()

        Else

            'getFosTables()


        End If

    End Sub

 
    Sub getFosTables()
        Dim ds As New DataSet
        Dim ffa As New formFieldAggregator
        Dim dt1 As New DataTable
        Dim dt2 As New DataTable
        Dim dt3 As New DataTable
        clearcache()
        ds = ffa.getfosLists()

        dt1 = ds.Tables(0)
        dt2 = ds.Tables(1)
        dt3 = ds.Tables(2)


        Cache.Insert("foslevel1", dt1, Nothing, DateTime.Now.AddMinutes(10), Caching.Cache.NoSlidingExpiration)
        Cache.Insert("foslevel2", dt2, Nothing, DateTime.Now.AddMinutes(10), Caching.Cache.NoSlidingExpiration)
        Cache.Insert("foslevel3", dt3, Nothing, DateTime.Now.AddMinutes(10), Caching.Cache.NoSlidingExpiration)


        If Cache("foslevel1") Is Nothing Then
            'muck out the cache
            clearcache()
            Cache("foslevel1") = dt1
            Cache("foslevel2") = dt2
            Cache("foslevel1") = dt3
        End If
       

        ddlCategory.DataSource = dt1.DefaultView
        ddlCategory.DataValueField = "FosID"
        ddlCategory.DataTextField = "FosDisplay"
        ddlCategory.DataBind()




    End Sub


    Sub clearcache()

        Cache.Remove("foslevel1")
        Cache.Remove("foslevel2")
        Cache.Remove("foslevel3")


    End Sub

    Private Function GetProductsByCategoryID(ByVal fosID As Integer) As String

        'Dim connectionString As String = "Server=localhost;Database=Northwind;Trusted_Connection=true"

        'Dim myConnection As SqlConnection = New SqlConnection(connectionString)

        'Dim myCommand As SqlCommand = New SqlCommand("SELECT * FROM Products WHERE CategoryID = @CategoryID", myConnection)

        'myCommand.Parameters.AddWithValue("@CategoryID", categoryID)

        ' Dim ad As SqlDataAdapter = New SqlDataAdapter(myCommand)

        'Dim ds As DataSet = New DataSet()

        'ad.Fill(ds)

        Dim dtin As New DataTable
        Dim dthold As New DataTable


        dtin = Cache("foslevel2")

        dthold = dtin


        Dim dvLevel2 As New DataView(dtin, "fosParentID = " & fosID & " and FosLevel = 2", "FosDisplay ASC", DataViewRowState.CurrentRows)

        Dim ddlProducts As DropDownList = New DropDownList()

        ddlProducts.ID = "ddlProducts"

        ddlProducts.DataSource = dvLevel2

        ddlProducts.Attributes.Add("onChange", "PopSelectedProductID(this.value)")

        ddlProducts.DataTextField = "FosDisplay"

        ddlProducts.DataValueField = "FosID"

        ddlProducts.DataBind()

        Dim sw As StringWriter = New StringWriter()

        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)

        ddlProducts.RenderControl(htw)

        ddProducts.Controls.Add(ddlProducts)



        Return sw.ToString()

        Cache("foslevel2") = dthold

    End Function


    Public Function GetCallbackResult() As String Implements _
    System.Web.UI.ICallbackEventHandler.GetCallbackResult
        If Int32.TryParse(argumentValue, 0) Then
            Return GetProductsByCategoryID(Int32.Parse(argumentValue))
        Else
            Return 0
        End If

    End Function

    Sub RaiseCallbackEvent(ByVal eventArgument As String) _
    Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        ' Dim parentId As Integer
        '
        If Int32.TryParse(eventArgument, 0) Then
            ' GetProductsByCategoryID(parentId)
            argumentValue = eventArgument
            '
        End If

    End Sub

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="cbtest.aspx.vb" Inherits="cbtest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script language="javascript">
  </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>


<h2>DropDownList Dependency Using ASP.NET 2.0 Client Callbacks</h2>


<div class="CategoryDropDownList">


Select a category:


<asp:DropDownList ID="ddlCategory" onChange="CallServer(this.options[selectedIndex].value)" runat="server" />


</div>


<div class="ProductsDropDownList">

<table>

<tr>

<td colspan="2">

Select a product: </td>
<td> <div id="MyDiv"><asp:PlaceHolder runat="server" id="ddProducts" />
</div>

</td>

</tr>

</table>

</div>


</div>


    </form>
</body>
</html>
markofsotonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Have you looked at the Atlas Control toolkit?  There is the CascadingDropdown control:

The "Atlas" Control Toolkit helps you bring your websites to life!
http://atlas.asp.net/default.aspx?tabid=47&subtabid=477

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.