Items showing up 2x in a listbox

B

Blasting Cap

I have some code where I populate some items in a listbox. The code is:


ListBoxDomesticExport.Items.Insert(0, "Domestic (Commercial)")
ListBoxDomesticExport.Items(0).Value = "0"

ListBoxDomesticExport.Items.Insert(1, "Domestic (All)")
ListBoxDomesticExport.Items(1).Value = "1"

ListBoxDomesticExport.Items.Insert(2, "Domestic (Non Commercial)")
ListBoxDomesticExport.Items(2).Value = "2"

ListBoxDomesticExport.Items.Insert(3, "Export")
ListBoxDomesticExport.Items(3).Value = "3"

ListBoxDomesticExport.Rows = 4


This works fine.


However, it populates the listbox 2 times, with the same data.

I only call the routine in 2 places. One is in the page_load event, below.



If Not Page.IsPostBack Then
setDomExport()
End If



The other is in a button click event, for a reset button.

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnReset.Click
setDomExport()
End Sub


Basically, in both places, I set other listboxes I have on that same
page, and set some indexes to -1.

When I run the page though, the routine SetDomExport() gets run twice,
no matter what.

Why is this doing this? Is there a way to make it NOT run it twice, or
is there a way to keep it from adding the same elements to the listbox a
second time?

Any help appreciated.

BC
 
S

sloan

If it were me, I'd recode the populate to this

private sub setDomExport
dim li as ListItem
li = new ListItem()
li.Text = Domestic (Commercial)"
li.Value = "0"
ListBoxDomesticExport.Items.Add(li)

li = new ListItem()
li.Text ="Domestic (All)"
li.Value = "1"
ListBoxDomesticExport.Items.Add(li)

''etc etc


end sub

That's just a "its more obvious" style thing......

Back to your question.

Can you do a

ListBoxDomesticExport.Clear() ''// or ListBoxDomesticExport.Items.Clear()
'' going from memory
when you call the reset button?
 
M

Mark Rae

If it were me, I'd recode the populate to this:

Private Sub setDomExport

ListBoxDomesticExport.Items.Clear()
ListBoxDomesticExport.Items.Add(new ListItem("Domestic (Commercial)", "0"))
ListBoxDomesticExport.Items.Add(new ListItem("Domestic (All)", "1"))
'etc

End Sub
 
B

bruce barker \(sqlwork.com\)

and if it was me, i'd turn viewstate off, and call the loader from oninit.

-- bruce (sqwork.com)
 
G

GroupReader

I agree w/ Bruce. Are you sure it's firing that function twice? Is it
possible that the values were loaded from viewstate, then loaded again
by you? Either way, you should turn off the viewstate if you don't
need it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top