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:
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 If
The 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
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!
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.
mhbrown
Member
48 Points
66 Posts
Dynamically Loading Custom User Control and Storing in Session...
Oct 19, 2011 08:41 PM|LINK
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) ThenProtocolDeviationForm = TryCast(Session("ProtocolDeviationForm"), ProtocolDeviationForm)
End If
The 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
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Dynamically Loading Custom User Control and Storing in Session...
Oct 21, 2011 02:34 AM|LINK
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
mhbrown
Member
48 Points
66 Posts
Re: Dynamically Loading Custom User Control and Storing in Session...
Oct 21, 2011 09:13 AM|LINK
Hi - thanks for your reply!
The problem is is that the usercontrol is loaded when a button is clicked.
Mark
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Dynamically Loading Custom User Control and Storing in Session...
Oct 21, 2011 09:18 AM|LINK
So you mean you don't want the usercontrol to load when you click the button?
mhbrown
Member
48 Points
66 Posts
Re: Dynamically Loading Custom User Control and Storing in Session...
Oct 21, 2011 09:22 AM|LINK
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!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Dynamically Loading Custom User Control and Storing in Session...
Oct 22, 2011 06:04 AM|LINK
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.