Having trouble adding a web user control programatically.

Discussion in 'ASP .Net Building Controls' started by Shawn, Jul 31, 2004.

  1. Shawn

    Shawn Guest

    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, Jul 31, 2004
    #1
    1. Advertising

  2. "Shawn" <> wrote in message
    news:...
    > 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.
    --
    John Saunders
    johnwsaundersiii at hotmail
     
    John Saunders, Jul 31, 2004
    #2
    1. Advertising

  3. Shawn

    Juno Guest

    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" <> wrote in message
    news:...
    > 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.
     
    Juno, Jul 31, 2004
    #3
  4. Shawn

    Shawn Guest

    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.

    "John Saunders" wrote:

    > "Shawn" <> wrote in message
    > news:...
    > > 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.
    > --
    > John Saunders
    > johnwsaundersiii at hotmail
    >
    >
    >
     
    Shawn, Jul 31, 2004
    #4
  5. "Shawn" <> wrote in message
    news:...
    > 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".
    --
    John Saunders
    johnwsaundersiii at hotmail

    > "John Saunders" wrote:
    >
    > > "Shawn" <> wrote in message
    > > news:...
    > > > 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.
    > > --
    > > John Saunders
    > > johnwsaundersiii at hotmail
    > >
    > >
    > >
     
    John Saunders, Aug 1, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. francois

    adding user control programatically

    francois, May 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    395
    Scott Allen
    May 19, 2004
  2. Shailesh Ratadia

    Re: adding user control programatically

    Shailesh Ratadia, May 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    487
    francois
    May 20, 2004
  3. Ben Dewey
    Replies:
    2
    Views:
    432
    Ben Dewey
    May 5, 2006
  4. Naya
    Replies:
    12
    Views:
    597
  5. Anand
    Replies:
    1
    Views:
    197
    Bob Barrows
    Feb 6, 2004
Loading...

Share This Page