Avatar of acdagirl
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...

E.g.
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 :-)
ASP.NET

Avatar of undefined
Last Comment
acdagirl

8/22/2022 - Mon
weichunglow

DropDownList1.Items.Clear()
acdagirl

ASKER
I know how to clear the ddl syntactically, but *where* does it need to be cleared - that was my question.
weichunglow

I assume that between your first ddl, there was a postback to load the 2nd ddl. During the loading, you should actually clear it.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
acdagirl

ASKER
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.
PatrickSweeney

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.
acdagirl

ASKER
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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
PatrickSweeney

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
acdagirl

ASKER
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>
                   <asp:DropDownList
                        ID="ddlGroup" runat="server" AutoPostBack="True"
                        DataSourceID="SqlDataSourceGroup"
                        DataTextField="group" DataValueField="group"                        
                    </asp:DropDownList>
               
        <asp:GridView ID="gridProduct" runat="server" AllowPaging="True"
            AutoGenerateColumns="False" DataSourceID="SqlDataSourceProduct" >
            <Columns>
                <asp:BoundField DataField="model codes " HeaderText="model codes "  />
                <asp:BoundField DataField="model description " HeaderText="model description "  />
                <asp:TemplateField HeaderText="Products">
                   <ItemTemplate>
                                      <asp:DropDownList ID="ddlProducts" runat="server"
                                      DataSourceID="SqlDataSourcetblproduct"
                                      DataTextField="product"
                                     DataValueField="prodguid"
                                     AutoPostBack="true" AppendDataBoundItems="True"
                                     OnSelectedIndexChanged="ddlProducts_SelectedIndexChanged">
                                   </asp:DropDownList>
              </ItemTemplate>
            </asp:TemplateField>
            </Columns>
         </asp:GridView>
         <asp:SqlDataSource ID="SqlDataSourceGroup" runat="server"
             ConnectionString="<omitted for brevity - it works>"
             SelectCommand="<omitted for brevity - it works>">
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSourceProduct" runat="server"
           ConnectionString="xxxxxx"
            SelectCommand="xxxxxx">
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSourcetblproduct" runat="server"
            ConnectionString="<xxxxx>"
            SelectCommand="xxxxx">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddlGroup" Name="group"           PropertyName="SelectedValue" Type="String" DefaultValue="gp1" />
            </SelectParameters>
        </asp:SqlDataSource>


The following onindex changed stuff works fine too...
    Protected Sub ddlProducts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            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
            Throw
        Catch ex As Exception
            lblError.Text = "An error occurred while attempting to update the row." & ex.Message & ex.StackTrace
        End Try
    End Sub
ASKER CERTIFIED SOLUTION
weichunglow

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
acdagirl

ASKER
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:

ddl.DataBind()

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)
            ddl.ClearSelection()
            ddl.DataBind()
        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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23