dynamic table viewstate problem

cacliffo
cacliffo used Ask the Experts™
on
Hello,

I have a table who's rows I need to populate dynamically based on the values selected in several drop down lists.

I have been calling the function to populate this table from Page_Load (since it depends on the values of other controls who's viewstate and postback data will not be loaded until Page_Load), like this:

        protected void Page_Load(object sender, EventArgs e)
        {
            populateQueueTable();
        }

This was working fine, until I tried to add sorting like this:
        protected void btnSort_Click(object sender, EventArgs e)
        {
            if (sender == btnDate)
            {
                hfSortType.Value = "1";
            }

            if (sender == btnAuthor)
            {
                hfSortType.Value = "2";
            }

            if (sender == btnFilename)
            {
                hfSortType.Value = "3";
            }

           
            while (tableQueue.Rows.Count > 1)
            {
                tableQueue.Rows.RemoveAt(1);
            }

            populateQueueTable();
           
        }

The problem is that on the first postback after the table is sorted (not including the postbock where the sort happens), the controls in my dynamic table lose their view state. After that postback, it works fine again.

It seems to me that there shouldn't be any problems loading view state because I am creating the same control hierarchy on every postback in Page_Load.

What is the problem here and how can I fix it?

Thanks,
Nick
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Create control in Page_Render

Author

Commented:
I tried putting it in Page_PreRender but then the viewstate doesn't get saved at all
Commented:
OK, I was able to fix this by explicitly assigning unique IDs (based on a key in the dataset I'm using to create the table) to the check boxes in my table that were losing the viewstate.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial