panel's controls collection lost during postback

G

Guest

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
 
K

Kikoz

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.
 
G

Guest

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.
 
K

Kikoz

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
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top