Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: page_load() executing twice


Message #1 by "Jim Henze" <henzej@t...> on Mon, 30 Sep 2002 20:29:03
Friends,
I have a problem with page_load() executing twice.  I have reviewed the 
archive and previous answers don't seem to apply.

I have stripped my program down to almost nothing to demonstrate the 
problem.  My page only has a listbox control that I created using Visual 
Studio .NET.  I have set AutoPostBack to true to respond to selection 
clicks.  This is what was generated by VS:


<%@ Page Language="vb" AutoEventWireup="false" 
Codebehind="WebForm1.aspx.vb" Inherits="ListBox2.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>WebForm1</title>
		<meta content="Microsoft Visual Studio.NET 7.0" 
name="GENERATOR">
		<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta 
content="http://schemas.microsoft.com/intellisense/ie5" 
name="vs_targetSchema">
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<asp:listbox id="lstEmployee" style="Z-INDEX: 101; 
LEFT: 38px; POSITION: absolute; TOP: 42px" runat="server" Height="204px" 
Width="148px" AutoPostBack="True"></asp:listbox></form>
	</body>
</HTML>

In my VB code, I fill the listbox with four entries.  The code looks like 
this:

Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents lstEmployee As System.Web.UI.WebControls.ListBox

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub 
InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
        ' Fill the list box with 4 iems
        If Not IsPostBack Then
            lstEmployee.Items.Add("item1")
            lstEmployee.Items.Add("item2")
            lstEmployee.Items.Add("item3")
            lstEmployee.Items.Add("item4")
        End If

    End Sub

    Private Sub lstEmployee_SelectedIndexChanged(ByVal sender As 
System.Object, ByVal e As System.EventArgs) Handles 
lstEmployee.SelectedIndexChanged
        ' Selection code here
    End Sub
End Class

When I run the application (using the debugger to see what is happening), 
I find that the initial load goes as expected as does the first selection 
click.  On the second and all subsequent selection clicks, Page_Load and 
lstEmployee_SelectedIndexChange are both executed twice.  

I have tired a variety of settings but I can't seen to get this to work 
without breaking something else.

Can anybody help?

Thanks,
Jim Henze

  Return to Index