Link to home
Start Free TrialLog in
Avatar of acdagirl

asked on

ddl keeps growing!

I have a dll within a gridview that gets populated based on the value of a ddl external to the dataview. The display and selected item index all seem fine when selections are made, however my ddl within the gridview appears to be caching or keeping the values of the old selection, so keeps growing each time a postback occurs...

The external/independent ddl contains: "cat, dog". My internally bound ddl is now blank.

I select "cat" from my external ddl and get "brown, white" in my gridview embedded ddl. I select "brown" and get my required info.

I decide to select "dog" from the external ddl. I now get "brown, white" (from "cat" selection) as well as "dog" selections "pit, boxer"...

The list just keeps growing the more selections I make. Where do I clear this ddl? I've tried on postback, within a slew of event handlers and get nowhere... should I give up and just clear all controls on the page within postback?

Any help or explanation would be appreciated...

Thanks :-)
Avatar of weichunglow

Avatar of acdagirl


I know how to clear the ddl syntactically, but *where* does it need to be cleared - that was my question.
I assume that between your first ddl, there was a postback to load the 2nd ddl. During the loading, you should actually clear it.
yes, there is a postback from external to internal ddl, however where is it best to clear the selection? I have tried many places but no effect...

i.e. on postback I need to "find" the control representing the ddl within the grid and when I tried that it didn't recognize it (I probably did something syntactically wrong)? I also tried during the databound event(s) of the gridview but no luck... the problem is which event to clear it in (with a !postback clause). I've tried a few but none seem to work.
Is this of any use?:

Dim index As Integer = GridView.EditIndex
Dim row As GridViewRow = GridView.Rows(index)
Dim dropdownlistobjectvariable As DropDownList = CType(row.FindControl("dropdownname"), DropDownList)

'dropdownlistobjectvariable is now the dropdownlist called dropdownnane in the row being edited.
Once again, where do I put any of this code? I know how to find the ddl values and/or indices... I can't clear the ddl on any page load from previous selections. Logically I need to find out *where* is the best place to clear it - not how.
Instead of appending items to the dropdownlist when you make the selection in the external ddl, why not have the content of the internal dropdownlist be based on a table query with a parameter of the selection in the external dropdown.

Have a table with
Type:       Item:
Dog        Pit Bull
Cat         White
Cat        Brown

Query it with

Select item from table where type = @type

And define @type as an asp:controlparameter
I am not appending anything?

I currently have the contents of the internal ddl based on a data source with a select statment based on the external ddl selection.

My problem is that ddlProducts keeps growing with the values from ddlGroup. Please see code snippet below (yes, I omitted some formatting things, etc for brevity but the main logic is here).

Where do I need to clear ddlProducts so that it stops growing on page load?

Here's part of the code:
                 <asp:Label ID="lblGroup" runat="server" ></asp:Label>
                        ID="ddlGroup" runat="server" AutoPostBack="True"
                        DataTextField="group" DataValueField="group"                        
        <asp:GridView ID="gridProduct" runat="server" AllowPaging="True"
            AutoGenerateColumns="False" DataSourceID="SqlDataSourceProduct" >
                <asp:BoundField DataField="model codes " HeaderText="model codes "  />
                <asp:BoundField DataField="model description " HeaderText="model description "  />
                <asp:TemplateField HeaderText="Products">
                                      <asp:DropDownList ID="ddlProducts" runat="server"
                                     AutoPostBack="true" AppendDataBoundItems="True"
         <asp:SqlDataSource ID="SqlDataSourceGroup" runat="server"
             ConnectionString="<omitted for brevity - it works>"
             SelectCommand="<omitted for brevity - it works>">
        <asp:SqlDataSource ID="SqlDataSourceProduct" runat="server"
        <asp:SqlDataSource ID="SqlDataSourcetblproduct" runat="server"
                <asp:ControlParameter ControlID="ddlGroup" Name="group"           PropertyName="SelectedValue" Type="String" DefaultValue="gp1" />

The following onindex changed stuff works fine too...
    Protected Sub ddlProducts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim query As StringBuilder = New StringBuilder()
            Dim strModelCode As String = gridProduct.Rows(0).Cells(0).Text

            Dim ddl As DropDownList = CType(gridProduct.Rows(0).Cells(2).Controls(1).FindControl("ddlProducts"), DropDownList)
            Dim strProduct As String = ddl.SelectedValue

            Response.Write("_ model code, product: " & strModelCode & ", " & strProduct)
           ' I get the values no problem
           ' here I start the custom query for update... no problem with this.... etc

        Catch ex As SqlClient.SqlException
        Catch ex As Exception
            lblError.Text = "An error occurred while attempting to update the row." & ex.Message & ex.StackTrace
        End Try
    End Sub
Avatar of weichunglow

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Argh! It didn't work but maybe I didn't do it right - would you please correct my steps?

1. in my ddlProducts_SelectedIndexChanged I added:


2. In the gridview databound event I did the following:

 Protected Sub gridProduct_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        If Page.IsPostBack Then
            Dim ddl As DropDownList = CType(gridProduct.Rows(0).Cells(2).Controls(1).FindControl("ddlProducts"), DropDownList)
        End If
    End Sub

3. I added the ddl databind to the end of (2) - is this the right place? It seems that it's simply not going through any steps in the databound event.... when I add a Response write nothing happens, and during debugging it doesn't hit the breakpoint? Is something wrong?