Page_Load not firing for SIMPLE Dynamic User Controls

Discussion in 'ASP .Net' started by jonelling, Jun 29, 2005.

  1. jonelling

    jonelling Guest

    I am having a problem where the page load event is not being fired
    for certain user controls that I load dynamically in placeholders.
    Here is what I'm doing in brief, with full test code supplied at the
    bottom.

    In my controlling .aspx page, in the page_init() sub, if not postback,
    I load two user controls and place them in placeholders. This works
    and a trace statement in each user control's page load handler is
    executed on initial page load. Then, using a basic button in the
    ..aspx, I post back the page. At this point, page_load runs only in one
    of the user controls. The weird thing here is that these usercontrols
    are identical except that one has a datagrid control in it and the
    other has a label control in it. If I remove the datagrid control from
    the control where page_load always runs, it stops working; conversely,
    if I add it to the other user control, page_load now runs on every
    postback.

    I am storing each usercontrol in session and retrieving and reinserting
    it on postback (not by my design), but I would think that if this was
    the problem, page_load wouldn't fire on postback for either
    usercontrol. Also, autoeventwireup is set to false for all components.

    Can any of you gurus help me out here?

    Thanks,
    Jelling

    Full Code, minus the html I couldn't paste in.

    PageLoadTest.aspx.vb

    Public Class PageLoadTest

    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web
    Form Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object

    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    Protected WithEvents ph1 As System.Web.UI.WebControls.PlaceHolder

    Protected WithEvents ph2 As System.Web.UI.WebControls.PlaceHolder

    Protected WithEvents crpersontype1 As CRPersonType

    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()

    Dim ctrl, ctrl2 As UserControl

    If Not Page.IsPostBack Then

    ctrl = New UserControl

    ctrl = Page.LoadControl("~/clientcenter/user/test.ascx")

    ctrl.ID = "crTest1"

    Session("PageLoadTest_ctrl") = ctrl

    ctrl2 = New UserControl

    ctrl2 = Page.LoadControl("~/ClientCenter/User/test2.ascx")

    ctrl2.ID = "crTest2"

    Session("PageLoadTest_ctrl2") = ctrl2

    ph1.Controls.Clear()

    ph1.Controls.Add(ctrl)

    ph2.Controls.Clear()

    ph2.Controls.Add(ctrl2)

    Else

    ctrl = CType(Session("PageLoadTest_ctrl"), test)

    ctrl2 = CType(Session("PageLoadTest_ctrl2"), test2)

    ph1.Controls.Clear()

    ph1.Controls.Add(ctrl)

    ph2.Controls.Clear()

    ph2.Controls.Add(ctrl2)

    End If

    End Sub

    #End Region

    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


    End Sub


    End Class




    Test.ascx.vb:


    Public Class test

    Inherits System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web
    Form Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object

    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

    'Put user code to initialize the page here

    Trace.Write("test.ascx pageload()")

    End Sub

    End Class


    Test2.ascx.vb:



    Public Class test2

    Inherits BaseClientCenterUserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web
    Form Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object

    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

    'Put user code to initialize the page here

    Trace.Write("test2.ascx pageload()")

    End Sub

    End Class
     
    jonelling, Jun 29, 2005
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.