Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Page.ClientScript.RegisterStartupScript & Threads

[es] :: .NET :: ASP.NET :: Page.ClientScript.RegisterStartupScript & Threads

[ Pregleda: 314 | Odgovora: 0 ]

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

VerbatimBOT
Aleksandar Dragosavac
Senior .NET Developer
Serbia, Belgrade

Član broj: 84129
Poruke: 224
77.46.215.*

Sajt: https://www.xing.com/prof..


Profil

icon Page.ClientScript.RegisterStartupScript & Threads23.07.2008. u 15:57

Pozdrav svima,
Imam jedan problem sa generisanjem JavaScripta preko RegisterStartupScript metode. Evo teksta sa Wrox-a, malo me mrzi da prevodim. :)


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:

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.


Pozdrav
Winners never quit, quitters never win.
23.07.2008. u 15:57 

[es] :: .NET :: ASP.NET :: Page.ClientScript.RegisterStartupScript & Threads

[ Pregleda: 314 | Odgovora: 0 ]

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.