?
Solved

Listbox keeps filling up with more items each time command button is pushed

Posted on 2005-05-17
12
Medium Priority
?
250 Views
Last Modified: 2010-04-07
Gurus

My Listbox keeps filling up with more items each time the command button is pushed.
All i want to happen is for the user to select an item and click the command button to process a procedure.
Yet, each time a button is pushed all of the items in page load keep getting added to the listbox

Here's whats in the page load:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        ListBox1.Items.Add("Add an Account")
        ListBox1.Items.Add("Update an Account")
        ListBox1.Items.Add("View an Account")
    End Sub

Here's whats in the command button:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Answer
        Answer = ListBox1.SelectedValue
        RegisterClientScriptBlock("AlertMessage", "<script language=Javascript>alert('The command was successfull!');</script>")

Can anyone clue me in?

Thank you

Ted


    End Sub
0
Comment
Question by:tcalbaz
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 23

Accepted Solution

by:
b1xml2 earned 1000 total points
ID: 14020366
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      If Not IsPostBack Then
      'Put user code to initialize the page here
            ListBox1.Items.Add("Add an Account")
            ListBox1.Items.Add("Update an Account")
            ListBox1.Items.Add("View an Account")
      End If
End Sub
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 14020379
You see the Page_Load is fired off everytime the page is requested, this includes postbacks. To ensure that your controls are prepopulated only at the initial load of the page, use the Not IsPostBack expression. Otherwise, at every postback, the list just gets added and added ad infinitum
0
 
LVL 1

Author Comment

by:tcalbaz
ID: 14020403
b1xml2,
Where do I use the Not IsPostBack expression?

Thank you

Ted
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:vadivhere
ID: 14020434
In the <@Page directive, add AutoEventFireup = "false" (in the aspx file, not in vb or cs file)

Cheers!
0
 
LVL 1

Author Comment

by:tcalbaz
ID: 14020527
vadivhere

Could you tell me where this directive is to be located or where I could find it?
I just did a search within the aspx file and couldn't find anything labled that way.

thank you

Ted
0
 
LVL 9

Expert Comment

by:hismightiness
ID: 14020581
tcalbaz, the IsPostback can be used nearly anywhere but in this example, you want it to control the flow to the population to your item population.  So, use b1xml2's example and put the if statement just before your item population.  Alternatively, if you have more than one databound control, you might want to have their databinding statements all contained in this IF statement (that is, as long as they are not supposed to be re-bound).
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 14020618
Ted, look at the code I posted, it is a fixed version of yours.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 14020623
vadivhere, incorrect answer. stop confusing the asker!
0
 
LVL 6

Expert Comment

by:Havagan
ID: 14020839
tcal:

b1xml2's initial answer is the correct one.

As an FYI, you can also call ListBox1.Items.Clear to remove all listbox items. However, it's less efficient to delete items and reload them each time the page loads rather than simply use b1's IsPostBack check to only load the listbox items the first time the page is loaded.

Paul
0
 
LVL 9

Expert Comment

by:hismightiness
ID: 14020918
Well, I think the "expert" consensus favors b1xml2's answer...  Hehehe...
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 14020974
<DO NOT ACCEPT THIS AS AN ANSWER>

b1xml2's answer, from above:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     If Not IsPostBack Then
     'Put user code to initialize the page here
          ListBox1.Items.Add("Add an Account")
          ListBox1.Items.Add("Update an Account")
          ListBox1.Items.Add("View an Account")
     End If
End Sub

is correct.  The VERY FIRST time the page is loaded, when it is to be displayed the VERY FIRST TIME, IsPostBack is False (the page is NOT being Posted Back), so the code to populate the ListBox is executed, and the listBox is filled.

When you do ANYTHING on the web-page (such as clicking on a command button), then the page IS POSTED BACK to the server, in which case IsPostBack is now TRUE, but the Page_Load procedure is called again (to load the page's code back onto the Server), and this time the IsPostBack flag is TRUE (the page IS A POSTBACK), so the code in the Page_Load procedure is NOT executed (because of the failure of the If....Then test), and the code to handle the CommandButton_Click event IS processed.

</DO NOT ACCEPT THIS AS AN ANSWER>

AW
0
 
LVL 1

Author Comment

by:tcalbaz
ID: 14020987
Guru's
I concur that b1xml2's initial answer is the correct one.
Thank you all for your sense of helpfulness and fairness.

Ted

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

850 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