samy
samy

Reputation: 1969

ASP.NET:adding controls Dynamicly afther post back with diffrent data

i got a page where the admin can edit some photos i pull from flickr so all the controls are added Dynamiclly.

i have few controls i add to a panel and then add the panel to Form.Controls. all the data thay are holding come from flickr.

my problem is that, afther a post back the list change and the controls are loading, BUT the Text Box's keep that same data befor the post back even though i get the data from flickr every time.

what wierd for my that if i do this with less textbox every thing work fine. the list are about 10+ pictures and this numbers can change in the future.

(sorry for the messy code im pretty new to programming) here is the code:

protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            RadioButtonList1.AutoPostBack = true;

            selectedSetID = getSelectedSetId(RadioButtonList1.SelectedItem.Text);

            photoSet = new MyPhotoSet(selectedSetID);
            photo = new FlickerImages[photoSet.Count];

            for (int j = 0; j < photo.Length; j++)
            {
                photo[j] = new FlickerImages(photoSet.MediumURLS[j], photoSet.ThumbnailURLS[j],                                 photoSet.Titles[j], photoSet.Descreption[j], photoSet.PhotosID[j]);

                panel = photo[j].GetPanelWithEditControls(j);

                button = new Button();
                button.ID = "sendDataButton" + j;
                button.Text = "send data";
                button.Click += button_Click;

                panel.Controls.Add(button);
                Form.Controls.Add(panel);
            }

        }
        else
        {
                 //here is the post back,
 every thing load fine.
 adding and removing the controls but if the textbox's load in the same place, it hold the same data befor the post back.

            RadioButtonList1.AutoPostBack = true;
            selectedSetID = getSelectedSetId(RadioButtonList1.SelectedItem.Text);

            photoSet = new MyPhotoSet(selectedSetID);
            photo = new FlickerImages[photoSet.Count];

            for (int i = 0; i < photo.Length; i++)
            {
                photo[i] = new FlickerImages(photoSet.MediumURLS[i], photoSet.ThumbnailURLS[i],                                     photoSet.Titles[i], photoSet.Descreption[i], photoSet.PhotosID[i]);

                panel = photo[i].GetPanelWithEditControls(i);

                button = new Button();
                button.ID = "sendDataButton" + i;
                button.Text = "send data";
                button.Click += button_Click;

                panel.Controls.Add(button);
                Form.Controls.Add(panel);
            }

        }
    }

(sorry for my english)

Upvotes: 1

Views: 749

Answers (1)

David
David

Reputation: 73564

Instead of putting your control generation in Page_Load, put it in Page_Init.

Since you mentioned that you're pretty new to ASP.NET, I strongly recommend reading up on the Page Lifecycle.

As an ASP.NET developer, it's very important that you understand how and when certain things happen on the server in between the time the server receives a request for an aspx page, and the time the page is output to be rendered by the browser.

The short explanation for why I suggest putting the code in Page_Init is how the runtime deals with dynamically created controls and Viewstate. The Viewstate contains any changes made by the user of your web app. In the page lifecycle, you'll notice that the application of Viewstate values happen before Page_Load, but after Page_Init.

This means that if your code in in Page_Load, then the controls are generated from Viewstate, updated with what the user put in, but then your own code in page_Load is re-generating those controls with the default values. There's nothing that happens after this event that can re-load the user's input.

(Hopefully that makes sense. If not, the page I linked to may explain it better.)

There's another article on here that addresses the subject from a different angle, but still should be very helpful for you.

And there's the ultra-short version here.

And finally, (saving the best for last) one of the most comprehensive articles on the subject that explains things in plain English can be found here.

Upvotes: 4

Related Questions