Subject: Page.ClientScript.RegisterStartupScript & Threads
Posted By: VerbatimBOT Post Date: 7/23/2008 5:39:06 AM
Hi all,
I would like to ask you about an issue I'm experiencing.
I have made a user control which represents a multiple dropdown list with some suboptions (http://img337.imageshack.us/img337/8284/56692765xe3.jpg).
Now, in order to save the state of these three instances on my page, I am using a hidden field for each of them, which stores all the selected IDs and when PostBack happens, I simply fire a JavaScript function which automatically selects all the checkboxes which were selected by the user previously. I hope I made this part clear enough.

Here is the code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            string[] selectedValues = this.SelectedValues;

            // the state is being cleared, because it will be set again by the JavaScript function
            ClearSelectedValues();

            System.Threading.Thread.Sleep(20);
            // set the state back to the selected one
            if (selectedValues.Length > 0)
            {
                string arrayOfValues = GenerateId();
                string script = "var " + arrayOfValues + " = new Array(" + selectedValues.Length + "); ";
                for (int i = 0; i < selectedValues.Length; i++)
                {
                    script += arrayOfValues + "[" + i + "] = " + selectedValues[i] + "; ";
                }
                script += " setCheckedState('chk_" + this.ID + "', " + arrayOfValues + "); ";

                Page.ClientScript.RegisterStartupScript(this.GetType(), arrayOfValues,
                   script, true);
            }
        }
    }


Somehow it only works for the first user control - the first one only manages to save its state. The solution I found is to put the current Thread to sleep for 20 miliseconds.
Does anyone have an explanation why is this working only with Thread.Sleep()?



-----------------------------------
Added:
-----------------------------------
I would just like add that it works fine without Thread.Sleep() method when I debug code line by line, which is the strangest thing.





Thanks in advance,

Aleksandar Dragosavac
Belgrade, Serbia

Go to topic 72926

Return to index page 1