Usercontrol in Page_Load

  • Thread starter Thread starter Big Charles
  • Start date Start date
B

Big Charles

Hello,

I don't know how UserControl should work:

If I put MyPlaceHolder.Controls.Add(MyUsrControl) inside "If (Not
Page.IsPostBack) Then", when MyWebForm is loaded, the usercontrol is
not loaded.
Why not?

Example:

Public Class MyWebForm
Inherits System.Web.UI.Page

Private MyUsrControl As Control =
LoadControl("../myUsrControl.ascx")

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If (Not Page.IsPostBack) Then
MyPlaceHolder.Controls.Add(MyUsrControl) ' Does not work.
Why?
End If

MyPlaceHolder.Controls.Add(MyUsrControl) ' Here it works
End Sub
 
You have to add the control to the page every time, not just the first
time the page is loaded.
 
Back
Top