Having trouble adding a web user control programatically.

S

Shawn

Hi All;

I am attempting to figure out how to add a user created control (test.ascx) to an ASP.NET page based on a user choice. I have created a simple page that uses session to keep track of the instantiated class. i have added to the class a simple public property (action) initially set to zero. i also have a panel on the page where the control will apear. i use this code to add the control to the panel.

Panel1.Controls.Add(myTest);

when a certain condition is raised the Action property is changed and when the page is posted back a check on that property shoud make the control appear. however when i run the code and the condition is met the control never appears. i know that the action property is being updated because i am outputing it to the page. any ideas what i could be doing wrong?
Thanks for any help.
 
J

John Saunders

Shawn said:
Hi All;

I am attempting to figure out how to add a user created control
(test.ascx) to an ASP.NET page based on a user choice. I have created a
simple page that uses session to keep track of the instantiated class. i
have added to the class a simple public property (action) initially set to
zero. i also have a panel on the page where the control will apear. i use
this code to add the control to the panel.
Panel1.Controls.Add(myTest);

when a certain condition is raised the Action property is changed and when
the page is posted back a check on that property shoud make the control
appear. however when i run the code and the condition is met the control
never appears. i know that the action property is being updated because i am
outputing it to the page. any ideas what i could be doing wrong?
Thanks for any help.

Shawn,

I can't really tell what you're doing from the description you gave, but
here are a few guesses:

When you are using dynamic controls, you have to create and add them, in the
same order, on every request. This means that no matter what the value of
Page.IsPostBack, you have to add the same controls in the same order.

In order to control the visibility of such controls, use the Visible
property. Setting Visible = False will not only make the control
"invisible", it will prevent the control from rendering any HTML at all.

A question: when you say you use session "to keep track of the instantiated
class", what class are you talking about? The user control? If so, I don't
think you should persist a user control in session state. Among other
things, you're meant to be able to update a user control simply by replacing
the .ascx file (assuming that the user control has no codebehind). If the
user control were persisted to Session state, such an update would not
update what's in Session.
 
J

Juno

Hi,

You should use loadcontrol first and then add the control to panel.
Like:
Dim myControl1 As MyControl =
CType(LoadControl("TempControl.ascx"),MyControl)
Panel1.Add(myControl1)


--
Juno
MCSD.NET, MCDBA, MCSE
----------------------------------------------------------
Support Team of EasyDotNet, INC. http://www.EasyDotNet.com
DataForm.NET - The most powerful data entry web server control for ASP.NET



Shawn said:
Hi All;

I am attempting to figure out how to add a user created control
(test.ascx) to an ASP.NET page based on a user choice. I have created a
simple page that uses session to keep track of the instantiated class. i
have added to the class a simple public property (action) initially set to
zero. i also have a panel on the page where the control will apear. i use
this code to add the control to the panel.
Panel1.Controls.Add(myTest);

when a certain condition is raised the Action property is changed and when
the page is posted back a check on that property shoud make the control
appear. however when i run the code and the condition is met the control
never appears. i know that the action property is being updated because i am
outputing it to the page. any ideas what i could be doing wrong?
 
S

Shawn

Hi John;
Actually it's a class i created to keep track of certain data that would be used throughout the life of the application for the user. i instantiate the class when the application starts and through the session i keep track of the class. the user created control is a different thing altogether. i want to be able to display the control based on what the user chooses.
 
J

John Saunders

Shawn said:
Hi John;
Actually it's a class i created to keep track of certain data that would
be used throughout the life of the application for the user. i instantiate
the class when the application starts and through the session i keep track
of the class. the user created control is a different thing altogether. i
want to be able to display the control based on what the user chooses.


Ok, then, as I said, you should control visibility with the Visible
property.

BTW, one reason I wasn't sure what you meant is that you used the term
"class" above when you should have said "object" or "class instance".
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top