QPR
asked on
Drop down list items appended on postback (need replaced)
I have 2 drop down boxes. When a selection is made from the first one then the second is populated based on the selection. If the selection is changed in the first drop down then the second list has the items for the new selection but also retains the items from previous selections.
I want all the items in DDL2 to be wiped and replaced with the new items not appended.
Did that make sense?
I have tried using
Protected Sub DDGroups_SelectedIndexChan ged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDGroups.SelectedIndexChan ged
DDCats.ClearSelection()
DDCats.DataBind()
End Sub
But no good
I want all the items in DDL2 to be wiped and replaced with the new items not appended.
Did that make sense?
I have tried using
Protected Sub DDGroups_SelectedIndexChan
DDCats.ClearSelection()
DDCats.DataBind()
End Sub
But no good
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
"AppendDataBoundItems is set to false "
this is set to true as I need to add a default value.
<asp:ListItem Value="0" Text="Category"></asp:List Item>
I use this because I don't want them to not select a category which would have the affect of selecting the top one by default. My required field validator has an initial value of 0
this is set to true as I need to add a default value.
<asp:ListItem Value="0" Text="Category"></asp:List
I use this because I don't want them to not select a category which would have the affect of selecting the top one by default. My required field validator has an initial value of 0
ASKER
DDCats.Items.Clear()
works perfectly - now if I could get it to re-append my default item...
works perfectly - now if I could get it to re-append my default item...
Before you clear DDCats save the default item.
I'm going to assume its your first item. So something like ListItem liDefault = DDCats.Items(0);
Then you can add the item back to DDCats after you clear it
-M3mph15
I'm going to assume its your first item. So something like ListItem liDefault = DDCats.Items(0);
Then you can add the item back to DDCats after you clear it
-M3mph15
ASKER
How do i save the default item?
What I need is a code behind version of this:
<asp:ListItem Value="0" Text="Category"></asp:List Item>
I have tried
DDCats.Items.Clear()
DDCats.Items.Add("Category ")
DDCats.DataBind()
which puts "category" at the top but it has no value and so slips past the validation
What I need is a code behind version of this:
<asp:ListItem Value="0" Text="Category"></asp:List
I have tried
DDCats.Items.Clear()
DDCats.Items.Add("Category
DDCats.DataBind()
which puts "category" at the top but it has no value and so slips past the validation
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
looks good thanks I'll try it when at work tomorrow.
"Select a category" has the value 0
This will always be zero as the categories from the DB have a CatID and that CatID is an identity field which can never be zero. By using a required field validator and saying the initial value = 0 I can always be sure that a user has not overlooked the drop down list and is required to select a proper category from it before submitting.
"Select a category" has the value 0
This will always be zero as the categories from the DB have a CatID and that CatID is an identity field which can never be zero. By using a required field validator and saying the initial value = 0 I can always be sure that a user has not overlooked the drop down list and is required to select a proper category from it before submitting.
ASKER
Had to alter the first line as am using vb.net but this (below) works perfectly thanks!
Dim li = New ListItem()
li.Text = "Category"
li.Value = "0"
DDCats.Items.Clear()
DDCats.Items.Add(li)
DDCats.DataBind()
Dim li = New ListItem()
li.Text = "Category"
li.Value = "0"
DDCats.Items.Clear()
DDCats.Items.Add(li)
DDCats.DataBind()
Firstly make sure that AppendDataBoundItems is set to false for the dropdownlist.
Also if the automatic methods do work, you can do manually:
For i = 0 To DDCats.Items.Count - 1
DDCats.Items.RemoveAt(0)
Next
This will remove all items.
HTH
-M3mph15