Solved

Drop down list items appended on postback (need replaced)

Posted on 2009-05-13
9
264 Views
Last Modified: 2012-08-13
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_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDGroups.SelectedIndexChanged
        DDCats.ClearSelection()
        DDCats.DataBind()
    End Sub

But no good
0
Comment
Question by:QPR
  • 5
  • 3
9 Comments
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 150 total points
Comment Utility
try using DDCats.Items.Clear() - although I would have thought the databind would already clear the items.
0
 
LVL 6

Expert Comment

by:M3mph15
Comment Utility
Hi,

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
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
"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:ListItem>
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
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
DDCats.Items.Clear()
works perfectly - now if I could get it to re-append my default item...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:M3mph15
Comment Utility
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
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
How do i save the default item?
What I need is a code behind version of this:
<asp:ListItem Value="0" Text="Category"></asp:ListItem>

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
0
 
LVL 6

Accepted Solution

by:
M3mph15 earned 350 total points
Comment Utility
Ok 2 ways.

1 is by saving the value that is already there:

ListItem li = new ListItem()
li = DDCats.Items(<index>)  //index is the index of the default item...first item is index 0.
DDCats.Clear()
DDCats.Items.Add(li)
DDCats.DataBind()

2 is if the value of the default value is always the same then you can hardcode it...but i wouldn't recommend incase you want to change it:

ListItem li = new ListItem()
li.Text = "<text that is shown in the dropdownlist>"
li.Value = "<value>"
DDCats.Clear()
DDCats.Items.Add(li)
DDCats.DataBind()

HTH
-M3mph15

0
 
LVL 29

Author Comment

by:QPR
Comment Utility
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.
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
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()


0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now