Disappearing usercontrol

G

Grant Merwitz

I am using usercontrols to control the contant in my website.
Currently, depending on a parameter passed in the querystring, the
appropriate usercontrol will be loaded.

i.e.
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
Switch(Request.Params["P"])
{
case "1" :
Control uc = LoadControl("/controls/control1.ascx");
pnlContent.Controls.Add(uc);
break;
case "2" :
Control uc = LoadControl("/controls/control2.ascx");
pnlContent.Controls.Add(uc);
break;
}
}
}

But now, when one of these control calls an internal method, the page posts
back and the control disappears.
I understand this i because the control is loaded through the code and is
not loaded again in the postback.

I have solved this problem by keeping the control in a session during post
backs, but am not completely sure if this is the right way to do things.

I have also tried to viewstate the control, but get the error:
The type 'ASP.control1_ascx' must be marked as Serializable or have a
TypeConverter other than ReferenceConverter to be put in viewstate.
I have further tried to mark the control as Serializable which didn't seem
to help.

Can anyone suggest a better way of doing this, or a solution

Thanks

Grant

P.S. the control is not called control1.ascx, i just renamed it for this
example.
 
K

Karl Seguin

Typically you store the name of the control in the session or viewstate and
reuse that... something like:


string controlName = null;

if (!Page.IsPostBack){
switch(){
case "1":
controlName = "/controls/control1.ascx";
break;
case "2"
controlName = "/controls/control2.ascx";
break;
}
pnlContent.Controls.Add(Page.LoadControl(controlName));
ViewState.Add("lastControl", controlName);
}ELSE{
controlName = (string)ViewState["lastControl"];
if (controlName != null){
pnlContent.Controls.Add(Page.LoadControl(controlName));
}
}


Denis Bauer has an PlaceHolderControl which takes care of this, check it
out:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
(free)

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
G

Grant Merwitz

Brilliant, Thank You

So even if i'm reloading the control, the post back still holds strong.
I was under the impresson that if i had to reload the control, it would load
as if it were loading for the first time.
Hence why i tried to viewstate the whole control.

So i guess it all goes down to understanding how the .net page loads, and
that it will load the control, before attempting to do the postback within
the control.

Very interesting and thanks for your help


Karl Seguin said:
Typically you store the name of the control in the session or viewstate
and
reuse that... something like:


string controlName = null;

if (!Page.IsPostBack){
switch(){
case "1":
controlName = "/controls/control1.ascx";
break;
case "2"
controlName = "/controls/control2.ascx";
break;
}
pnlContent.Controls.Add(Page.LoadControl(controlName));
ViewState.Add("lastControl", controlName);
}ELSE{
controlName = (string)ViewState["lastControl"];
if (controlName != null){
pnlContent.Controls.Add(Page.LoadControl(controlName));
}
}


Denis Bauer has an PlaceHolderControl which takes care of this, check it
out:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
(free)

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Grant Merwitz said:
I am using usercontrols to control the contant in my website.
Currently, depending on a parameter passed in the querystring, the
appropriate usercontrol will be loaded.

i.e.
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
Switch(Request.Params["P"])
{
case "1" :
Control uc = LoadControl("/controls/control1.ascx");
pnlContent.Controls.Add(uc);
break;
case "2" :
Control uc = LoadControl("/controls/control2.ascx");
pnlContent.Controls.Add(uc);
break;
}
}
}

But now, when one of these control calls an internal method, the page posts
back and the control disappears.
I understand this i because the control is loaded through the code and is
not loaded again in the postback.

I have solved this problem by keeping the control in a session during
post
backs, but am not completely sure if this is the right way to do things.

I have also tried to viewstate the control, but get the error:
The type 'ASP.control1_ascx' must be marked as Serializable or have a
TypeConverter other than ReferenceConverter to be put in viewstate.
I have further tried to mark the control as Serializable which didn't
seem
to help.

Can anyone suggest a better way of doing this, or a solution

Thanks

Grant

P.S. the control is not called control1.ascx, i just renamed it for this
example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top