panel's controls collection lost during postback

Discussion in 'ASP .Net' started by =?Utf-8?B?RWQgQ2hpdQ==?=, Nov 17, 2004.

  1. Hi,

    I plan to use panel to store multiple HTML text in a web form page, here is
    the code snippet that add HTML into panel:

    Dim lit1 As New Literal
    lit1.Text = strHTML
    Panel1.Controls.Add(lit1)

    The problem I have is , all literal controls I added into Panel1.Controls
    are lost during the postback. Is there a way to overcome this?

    I have another question somehow related to this one. How do I determine the
    type at runtime, the following code snippet does not work:
    If Panel1.Controls(i).GetType = LiteralConrol Then

    TIA
     
    =?Utf-8?B?RWQgQ2hpdQ==?=, Nov 17, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?RWQgQ2hpdQ==?=

    Kikoz Guest

    Hi.

    You instantiate your Literal control and add it to the Page.Controls
    collection only when it's not a post back, right? I.e. (C#):

    if(!IsPostBack)
    {
    Literal lit1 = new ... blah blah
    }

    You shouldn't.


    "Ed Chiu" <> wrote in message
    news:...
    > Hi,
    >
    > I plan to use panel to store multiple HTML text in a web form page, here
    > is
    > the code snippet that add HTML into panel:
    >
    > Dim lit1 As New Literal
    > lit1.Text = strHTML
    > Panel1.Controls.Add(lit1)
    >
    > The problem I have is , all literal controls I added into Panel1.Controls
    > are lost during the postback. Is there a way to overcome this?
    >
    > I have another question somehow related to this one. How do I determine
    > the
    > type at runtime, the following code snippet does not work:
    > If Panel1.Controls(i).GetType = LiteralConrol Then
    >
    > TIA
    >
    >
     
    Kikoz, Nov 17, 2004
    #2
    1. Advertising

  3. Thank you for the reply. But the answer is no.
    The code snippet is not even in Page_load.

    I have 2 buttons: btnSave and btnLoad

    The code snippet is inside btnLoad_Click which is responsible for reading
    infomation from SQL server and generate HTML texts per each record. This part
    is working.

    But btnSave_Click is not, when I click this button all controls in the panel
    are lost. My guess is due to postback.







    "Kikoz" wrote:

    > Hi.
    >
    > You instantiate your Literal control and add it to the Page.Controls
    > collection only when it's not a post back, right? I.e. (C#):
    >
    > if(!IsPostBack)
    > {
    > Literal lit1 = new ... blah blah
    > }
    >
    > You shouldn't.
    >
    >
    > "Ed Chiu" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I plan to use panel to store multiple HTML text in a web form page, here
    > > is
    > > the code snippet that add HTML into panel:
    > >
    > > Dim lit1 As New Literal
    > > lit1.Text = strHTML
    > > Panel1.Controls.Add(lit1)
    > >
    > > The problem I have is , all literal controls I added into Panel1.Controls
    > > are lost during the postback. Is there a way to overcome this?
    > >
    > > I have another question somehow related to this one. How do I determine
    > > the
    > > type at runtime, the following code snippet does not work:
    > > If Panel1.Controls(i).GetType = LiteralConrol Then
    > >
    > > TIA
    > >
    > >

    >
    >
    >
     
    =?Utf-8?B?RWQgQ2hpdQ==?=, Nov 17, 2004
    #3
  4. =?Utf-8?B?RWQgQ2hpdQ==?=

    Kikoz Guest

    Sorry, at first I didn't realize you were talking about event handlers.
    Of course, it's due to post back. If you new control(s) has not been in the
    page when it first loaded and if they are not "global" to the page (not hard
    coded in HTML, for example) and if you add them based on some condition(s)
    while handling some post back event(s) your page is simply not aware of them
    during Init event which happens every time. Meaning ViewState doesn't know
    about them (if you use ViewState), etc.

    What you do is the following: have a private method of your page class that
    would create such control. Think about its correct implementation. And call
    it from both handlers. Although generally bad idea, sometimes it's ok to use
    ViewState, Cache or Session to store between post backs some key-value pairs
    of vital properties of controls that have been added to container "on the
    fly".

    BTW, I forgot about your second question. The correct code would be (C#
    again, sorry):

    if(Panel1.Controls(i).GetType().Name.ToLower() == "literalconrol")
    {
    // Blah blah
    }

    Kikoz

    "Ed Chiu" <> wrote in message
    news:...
    > Thank you for the reply. But the answer is no.
    > The code snippet is not even in Page_load.
    >
    > I have 2 buttons: btnSave and btnLoad
    >
    > The code snippet is inside btnLoad_Click which is responsible for reading
    > infomation from SQL server and generate HTML texts per each record. This
    > part
    > is working.
    >
    > But btnSave_Click is not, when I click this button all controls in the
    > panel
    > are lost. My guess is due to postback.
    >
    >
    >
    >
    >
    >
    >
    > "Kikoz" wrote:
    >
    >> Hi.
    >>
    >> You instantiate your Literal control and add it to the Page.Controls
    >> collection only when it's not a post back, right? I.e. (C#):
    >>
    >> if(!IsPostBack)
    >> {
    >> Literal lit1 = new ... blah blah
    >> }
    >>
    >> You shouldn't.
    >>
    >>
    >> "Ed Chiu" <> wrote in message
    >> news:...
    >> > Hi,
    >> >
    >> > I plan to use panel to store multiple HTML text in a web form page,
    >> > here
    >> > is
    >> > the code snippet that add HTML into panel:
    >> >
    >> > Dim lit1 As New Literal
    >> > lit1.Text = strHTML
    >> > Panel1.Controls.Add(lit1)
    >> >
    >> > The problem I have is , all literal controls I added into
    >> > Panel1.Controls
    >> > are lost during the postback. Is there a way to overcome this?
    >> >
    >> > I have another question somehow related to this one. How do I determine
    >> > the
    >> > type at runtime, the following code snippet does not work:
    >> > If Panel1.Controls(i).GetType = LiteralConrol Then
    >> >
    >> > TIA
    >> >
    >> >

    >>
    >>
    >>
     
    Kikoz, Nov 17, 2004
    #4
    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. NotYetaNurd
    Replies:
    1
    Views:
    486
    John Saunders
    Nov 7, 2003
  2. vcuankitdotnet

    Dynamic Controls Lost On Postback

    vcuankitdotnet, Apr 16, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    6,274
    Thomas Hansen
    Apr 18, 2007
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    1,013
    Øyvind Isaksen
    May 18, 2007
  4. Dejan

    Dynamically added controls are lost on a Postback

    Dejan, Nov 8, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    164
    John Saunders
    Nov 9, 2006
  5. Ruslan
    Replies:
    0
    Views:
    149
    Ruslan
    Nov 13, 2003
Loading...

Share This Page