Solved

VB.net How to execute Page_Load One time

Posted on 2016-09-26
2
40 Views
Last Modified: 2016-09-27
VB 2015

One the web page I have a list box that has 2 items added using Page_Load as follows

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ListBox1.Items.Add(New ListItem("Item1))
        ListBox1.Items.Add(New ListItem("Item2))
    End Sub

So the list box will have

Item1
Item2

In the same web page there is a button
Page_Load is executed every time a button is clicked

As a result List box is becoming

Item1
Item2
Item1
Item2

And Keeps on growing every time Button is clicked!    I discovered that Page_Load is loaded every time the button is clicked.
Is there a way to execute Page_Load once
0
Comment
Question by:saljas
2 Comments
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 41816382
You want to check to see if the page is posting back or not; e.g. -
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
	If Not Page.IsPostBack Then
		ListBox1.Items.Add(New ListItem("Item1))
		ListBox1.Items.Add(New ListItem("Item2))
	End If
End Sub

Open in new window


-saige-
0
 

Author Closing Comment

by:saljas
ID: 41817387
Many thanks
Exact answer
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

9 Experts available now in Live!

Get 1:1 Help Now