Click here to Skip to main content
15,913,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am loading usercontrol dynamically, which works fine, but anytime i click a button on the usercontrol it disappears below are my codes:

Dim meetingtypeControl As Control = LoadControl("~/Modules/CouncilAndGeneralAffairs/WebUserControls/Settings-CouncilMeetingType.ascx")
Dim subMeetingtypeControl As Control = LoadControl("~/Modules/CouncilAndGeneralAffairs/WebUserControls/Settings-SubCouncilMeetingSetup.ascx")

Protected Sub meetingTypeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles meetingTypeButton.Click
meetingSetupPlaceHolder.Controls.Clear()
meetingtypeControl.ID = "UC1"
meetingSetupPlaceHolder.Controls.Add(meetingtypeControl)
End Sub
Protected Sub subMeetingTypeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles subMeetingTypeButton.Click
meetingSetupPlaceHolder.Controls.Clear()
subMeetingtypeControl.ID = "UC2"
meetingSetupPlaceHolder.Controls.Add(subMeetingtypeControl)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
Dim meetingtypeControl As Control = LoadControl("~/Modules/CouncilAndGeneralAffairs/WebUserControls/Settings-CouncilMeetingType.ascx")
meetingSetupPlaceHolder.Controls.Add(meetingtypeControl)
End If
End Su
b
Posted

Please replace below code.

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

Dim meetingtypeControl As Control = LoadControl("~/Modules/CouncilAndGeneralAffairs/WebUserControls/Settings-CouncilMeetingType.ascx")
meetingSetupPlaceHolder.Controls.Add(meetingtypeControl)

End Sub


Please Vote if this helped you then.
 
Share this answer
 
Comments
Adesina Mark 7-Oct-10 6:15am    
It works when it loads under pageload, but when i load it under button_click event that is when it disappear after any postback. Any suggestion of the cause?
NMehta83 7-Oct-10 6:26am    
That is why i removed your condition Page.IsPostBack = False. So please replace the code and check it again.
C++
Every PostBack is a new Request.You need to add the Control with each PostBack


C#
private void Page_Load(object sender, EventArgs e)
{
   // Dynamically load the control here
   if (!Page.IsPostBack)
   {
    // Do dynamic initialization of controls 
       which need such initialization only once
   }
}


you can also refer this discussion: http://forums.asp.net/t/1186195.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900