Answered by:
Dynamically Loading Custom User Control and Storing in Session...

Question
-
User1823070883 posted
Hi all I have a usercontrol that has lots of form elements in it, textboxes, drop downs etc and two buttons - Save and Cancel. I am loading this usercontrol dynimically in the aspx's code behind like so in the pages PreLoad even (I have also tried; Init; PreInit and Load:
ProtocolDeviationForm = Page.LoadControl("~/Secure/GlobalUserControls/ProtocolDeviationForm.ascx")
If Not (Session("ProtocolDeviationForm") Is Nothing) Then
ProtocolDeviationForm = TryCast(Session("ProtocolDeviationForm"), ProtocolDeviationForm)
End If
ProtocolDeviationForm.ID = "ProtocolDeviationForm"
AddHandler ProtocolDeviationForm.UpdateProtocolDeviation, AddressOf UpdateProtocolDevaition
AddHandler ProtocolDeviationForm.CancelProtocolDeviation, AddressOf CancelProtocolDevaition
AddHandler ProtocolDeviationForm.SaveProtocolDeviation, AddressOf SaveProtocolDevaition
plcProtocolDeviationForm.Controls.Add(ProtocolDeviationForm)As you can see I am also storing the User Control in Session so that what properties have been assigned to it are stored. I am doing this like so:
ProtocolDeviationForm = LoadControl("~/Secure/GlobalUserControls/ProtocolDeviationForm.ascx")
ProtocolDeviationForm.ID = "ProtocolDeviationForm"
ProtocolDeviationForm.CilCode = cilCode
ProtocolDeviationForm.StudyLevelId = Request.QueryString("subjectId")
ProtocolDeviationForm.StudyLevel = StudyLevel.Subject
ProtocolDeviationForm.ProtocolDeviationId = CType(e.CommandArgument, Integer)
ProtocolDeviationForm.FormAction = FormAction.Add
Session("ProtocolDeviationForm") = ProtocolDeviationForm
plcProtocolDeviationForm.Controls.Add(ProtocolDeviationForm)However, when the button in the user control is clicked... two things happen depending on what is included in the first snippet of code:
If the following isn't included -
If Not (Session("ProtocolDeviationForm") Is Nothing) Then
ProtocolDeviationForm = TryCast(Session("ProtocolDeviationForm"), ProtocolDeviationForm)
End IfThe event for the button in the usercontrol is fired but the User Control properties are all blank (default values). However, if I include this line, the event for the button doesn't fire but the Control seems to be loaded from Session.
I am really stumped and have been working on this for a couple of days now...
Does anyone know what the problem is? Thanks in advance
Mark
Wednesday, October 19, 2011 4:41 PM
Answers
-
User3866881 posted
The event for the button in the usercontrol is fired but the User Control properties are all blank (default values).Hello again:)
Please make sure that your self-made propeties like Id or CilCode...,ect. are something stored in the ViewState instead of losting data contents:
public int ID
{
get
{
ViewState["id"]==null?0:(int)ViewState["id"];
}
set
{
ViewState["id"] = value;
}
}
You CANNOT define the property like:
public int Id{get;set;}
For everytime when the control is created and load, this property value will be recreate again with the newly-built control and all the previous data contents will be lost.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, October 22, 2011 2:04 AM
All replies
-
User3866881 posted
Hello:)
Have you tried to load the control in the “If Not IsPostBaclk……End If” block in the Page_Load.
And how did you write your user control?
Plz show us your full codes...
Thx very much
Thursday, October 20, 2011 10:34 PM -
User1823070883 posted
Hi - thanks for your reply!
The problem is is that the usercontrol is loaded when a button is clicked.
Mark
Friday, October 21, 2011 5:13 AM -
User3866881 posted
The problem is is that the usercontrol is loaded when a button is clicked.So you mean you don't want the usercontrol to load when you click the button?
Friday, October 21, 2011 5:18 AM -
User1823070883 posted
No I want it to load when the button is clicked. The problem would be if I put If Not Is Postback... as the button is a post back... therefore the UserControl won't load from Session nor will the events in the control be wired up!
Friday, October 21, 2011 5:22 AM -
User3866881 posted
The event for the button in the usercontrol is fired but the User Control properties are all blank (default values).Hello again:)
Please make sure that your self-made propeties like Id or CilCode...,ect. are something stored in the ViewState instead of losting data contents:
public int ID
{
get
{
ViewState["id"]==null?0:(int)ViewState["id"];
}
set
{
ViewState["id"] = value;
}
}
You CANNOT define the property like:
public int Id{get;set;}
For everytime when the control is created and load, this property value will be recreate again with the newly-built control and all the previous data contents will be lost.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, October 22, 2011 2:04 AM