Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > panel's controls collection lost during postback

Reply
Thread Tools

panel's controls collection lost during postback

 
 
=?Utf-8?B?RWQgQ2hpdQ==?=
Guest
Posts: n/a
 
      11-17-2004
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


 
Reply With Quote
 
 
 
 
Kikoz
Guest
Posts: n/a
 
      11-17-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
=?Utf-8?B?RWQgQ2hpdQ==?=
Guest
Posts: n/a
 
      11-17-2004
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>
>

 
Reply With Quote
 
Kikoz
Guest
Posts: n/a
 
      11-17-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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
>> >
>> >

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Disable web page controls during postback round-trip =?Utf-8?B?TGFycnk=?= ASP .Net 2 03-09-2006 04:05 PM
Placeholder's controls disappear during postback =?Utf-8?B?U2ltb24gV2FsbGlz?= ASP .Net 1 03-09-2005 08:17 PM
disabling controls during the postback... Dan =o\) ASP .Net 3 11-09-2004 03:01 PM
retain controls added to placeholder during postback? Mark ASP .Net 2 07-06-2004 05:53 PM



Advertisments