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 :-)
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 :-)
DropDownList1.Items.Clear( )
ASKER
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.
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.
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("dro pdownname" ), DropDownList)
'dropdownlistobjectvariabl e is now the dropdownlist called dropdownnane in the row being edited.
Dim index As Integer = GridView.EditIndex
Dim row As GridViewRow = GridView.Rows(index)
Dim dropdownlistobjectvariable
'dropdownlistobjectvariabl
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.
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
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
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="SqlDataSourc eGroup"
DataTextField="group" DataValueField="group"
</asp:DropDownList>
<asp:GridView ID="gridProduct" runat="server" AllowPaging="True"
AutoGenerateColumns="False " DataSourceID="SqlDataSourc eProduct" >
<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="SqlDataSourc etblproduc t"
DataTextField="product"
DataValueField="prodguid"
AutoPostBack="true" AppendDataBoundItems="True "
OnSelectedIndexChanged="dd lProducts_ SelectedIn dexChanged ">
</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="SqlDataSourcetblproduc t" runat="server"
ConnectionString="<xxxxx>"
SelectCommand="xxxxx">
<SelectParameters>
<asp:ControlParameter ControlID="ddlGroup" Name="group" PropertyName="SelectedValu e" Type="String" DefaultValue="gp1" />
</SelectParameters>
</asp:SqlDataSource>
The following onindex changed stuff works fine too...
Protected Sub ddlProducts_SelectedIndexC hanged(ByV al 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).C ontrols(1) .FindContr ol("ddlPro ducts"), 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
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="SqlDataSourc
DataTextField="group" DataValueField="group"
</asp:DropDownList>
<asp:GridView ID="gridProduct" runat="server" AllowPaging="True"
AutoGenerateColumns="False
<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="SqlDataSourc
DataTextField="product"
DataValueField="prodguid"
AutoPostBack="true" AppendDataBoundItems="True
OnSelectedIndexChanged="dd
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceGroup" runat="server"
ConnectionString="<omitted
SelectCommand="<omitted for brevity - it works>">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceProduct" runat="server"
ConnectionString="xxxxxx"
SelectCommand="xxxxxx">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourcetblproduc
ConnectionString="<xxxxx>"
SelectCommand="xxxxx">
<SelectParameters>
<asp:ControlParameter ControlID="ddlGroup" Name="group" PropertyName="SelectedValu
</SelectParameters>
</asp:SqlDataSource>
The following onindex changed stuff works fine too...
Protected Sub ddlProducts_SelectedIndexC
Try
Dim query As StringBuilder = New StringBuilder()
Dim strModelCode As String = gridProduct.Rows(0).Cells(
Dim ddl As DropDownList = CType(gridProduct.Rows(0).
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Argh! It didn't work but maybe I didn't do it right - would you please correct my steps?
1. in my ddlProducts_SelectedIndexC hanged I added:
ddl.DataBind()
2. In the gridview databound event I did the following:
Protected Sub gridProduct_DataBound(ByVa l sender As Object, ByVal e As System.EventArgs)
If Page.IsPostBack Then
Dim ddl As DropDownList = CType(gridProduct.Rows(0). Cells(2).C ontrols(1) .FindContr ol("ddlPro ducts"), 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?
1. in my ddlProducts_SelectedIndexC
ddl.DataBind()
2. In the gridview databound event I did the following:
Protected Sub gridProduct_DataBound(ByVa
If Page.IsPostBack Then
Dim ddl As DropDownList = CType(gridProduct.Rows(0).
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?