Solved

VB.net How to execute Page_Load One time

Posted on 2016-09-26
2
65 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 33

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…

685 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