Control.Add or Item.Add

  • Thread starter Thread starter David
  • Start date Start date
D

David

I am new to ASP.NET and am becoming frustrated.

I am able to add items to a drop-down list box (SELECT) and the items remain
across server calls. When I add controls to a DIV (or to the Form for that
matter) they are not retained. When I add HTML from the server using the
InnerHtml property of the DIV (a Generic Html control) the state is
maintained. What is the difference between InnerHtml += <img etc. > and
Controls.Add(new HtmlImage())? Why does one persist and the other not.

Thanks in advance.

David
 
Hi David,

The HtmlGenericControl stores its InnerHtml in ViewState. This is why it
persists. When you add a Control to it, you are not persisting that Control
in any way. The next PostBack, it's not there, because it wasn't persisted,
and you didn't re-add it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
Kevin Spencer said:
Hi David,

The HtmlGenericControl stores its InnerHtml in ViewState. This is why it
persists. When you add a Control to it, you are not persisting that
Control in any way. The next PostBack, it's not there, because it wasn't
persisted, and you didn't re-add it.

Thank you. But how can I get it to persist?
 

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

Back
Top