Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a main panel which is called linksAndPicturesPanel within this panel, I want to add more panels -> 1 panel for the link and 1 panel for a picture (red and green). enter image description hereFurthermore, it should be possible to add multiple links and pictures. To add those panels I use this code:

 Panel linkPanel = new Panel();
 linkPanel.CssClass = "linkList";
 linkPanel.Controls.Add(new LiteralControl("blablabla -----"));

 Panel picturePanel = new Panel();
 picturePanel.CssClass = "pictureFromLink";
 picturePanel.Controls.Add(new LiteralControl("blablabla22222 -----"));

 linksAndPicturesPanel.Controls.Add(linkPanel);
 linksAndPicturesPanel.Controls.Add(picturePanel);

This works fine for one line. After adding multiple lines, the page reloads and all lines exept the last one are gone. Is there a way, to prevent a page reload so that there all added items will be displayed ?

share|improve this question
    
Have you tried looking at ASP:UpdatePanel? –  Daniel Ziga Mar 18 '13 at 13:43

1 Answer 1

SharePoint UI is built as a Web Application. Web Application are Stateless and that is the reason why controls you are generating dynamically are lost on postback.

To eliminate this you should handle the Page_Load event and create the controls.

 protected void Page_Load(object sender, EventArgs e)
 {
    Panel linkPanel = new Panel();
    linkPanel.CssClass = "linkList";
    linkPanel.Controls.Add(new LiteralControl("blablabla -----"));

    Panel picturePanel = new Panel();
    picturePanel.CssClass = "pictureFromLink";
    picturePanel.Controls.Add(new LiteralControl("blablabla22222 -----"));

    linksAndPicturesPanel.Controls.Add(linkPanel);
    linksAndPicturesPanel.Controls.Add(picturePanel);
 }

Also you should use Session for saving the data during postback.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.