On Postback placeholder controls disappearing
-
22-01-2021 - |
Question
Within my webpage I am loading usercontrols within a placeholder. Each of these user controls triggers a postback when an ajaxcontroltoolkit rating is changed. The problem I am having is that if I use
If (Not IsPostBack is Nothing)
the controls within the placeholder disappear on post.
My Page_Load looks like this currently
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session("ProId") = Nothing
Session("FolId") = Nothing
Dim ProId As Integer
If (Not Request.QueryString("ProjectID") Is Nothing) Then
ProId = Convert.ToInt32(Request.QueryString("ProjectID").ToString())
Session("ProId") = Request.QueryString("ProjectID").ToString()
End If
Dim FolId As Integer
If (Not Request.QueryString("FolderID") Is Nothing) Then
FolId = Convert.ToInt32(Request.QueryString("FolderID").ToString())
Session("FolId") = Request.QueryString("FolderID").ToString()
End If
objUser = New BSSiteUser(CInt(Page.User.SiteUser.intID))
objProject = New BSProject(ProId)
objFolder = New BSFolder(objUser.SiteUserID, FolId)
objOrganization = New BSOrganization(objProject.intOrganizationID, objUser.SiteUserID)
Me.Load_SubcontractorList()
Me.Load_EvaluationList(1)
Me.Load_EvaluationList(2)
Me.lblorganization.Text = objOrganization.CompanyName
Me.lblprojectname.Text = objProject.strProjectName
Me.lblprojectnumber.Text = objProject.strProjectNumber
Me.lbldatecreated.Text = Date.Now.Date.ToString()
End Sub
The Load_EvaluationList is what loads the user controls, if I place the IsPostBack check around those two, the controls disappear, what could be the problem
Solution 3
I am abandoning this question as a decision was made to go a different route
OTHER TIPS
Dynamically added controls disappear on postback as you are working with a brand new instance of your page. In order for you to keep them you are going to have to recreate them for any postback that occurs.
It is also important to note that if you want your ViewState to be retained you should create your user controls on Page Init and not Page Load as loading of ViewState data back to the controls happens before the Load event.
If you are dynamically adding controls then on every postback you have to add control again.
try using if(Not IsPostBack) or (IsPostBack) instead of comparing it with Nothing.
Here I am simply adding a textbox dynamically on postback and it will stay in every postback.
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (IsPostBack) Then
Dim t As TextBox = New TextBox()
form1.Controls.Add(t)
End If
End Sub
End Class
You can put the controls you are trying to add dynamically in one function/method/subroutine and just call inside of Load.
Update
If you want your data to stay in the controls you have to save the data in the viewstate. You will have to create control again no matter what but before control is sent back to user get their data from the view state. It is described with example here.