javascript controls disappear

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omlac
    New Member
    • Aug 2008
    • 10

    javascript controls disappear

    im creating controls using javascript, but whenever there is a postback on the page all the controls created in javascript disappear.
    How do i fix this problem.
    the code below shows how im creating them.

    Code:
    var txt5   = document.createElement("input");
                  txt5.setAttribute ('type', 'text');
                  txt5.setAttribute ('id', j[0]);
                  txt5.setAttribute ('name', j[0]);
                  txt5.setAttribute ('text', j[0]);
                  var chk1 = document.createElement("input");
                  chk1.setAttribute ('type', 'checkbox');
                  chk1.setAttribute ('id', "chk" + j[0] );
                  chk1.setAttribute ('name',"chk" +  j[0]);
                  chk1.setAttribute ('text',"chk" +  j[0]); 
                  //chk1.innerHTML  = "Default";
                  var lbl1   = document.createElement('label');
                  lbl1.setAttribute ('id', "lblDefault" );
                  lbl1.innerHTML =  "Default";
                  lbl1.style.width = "30px";
                  var lbl3   = document.createElement('label');
                  lbl3.setAttribute ('id', "lbl" + j[0]);
                  lbl3.innerHTML =  j[0];
                  lbl3.style.width = "125px";              
                  txt5.style.top='15px';
                  txt5.style.left='15px';
                  var newdiv = document.createElement('div');
                  di.setAttribute('id',"divIdName");                            
                  di.appendChild(lbl3);
                  di.appendChild(txt5);
    Last edited by acoder; Mar 18 '09, 10:33 AM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You need to recreate them using the same code on page load.

    Comment

    • omlac
      New Member
      • Aug 2008
      • 10

      #3
      i added this line on init but it just runs trough the line and doesnt call the java function when its a postback, it only run once(the first time)

      ClientScript.Re gisterStartupSc ript(Me.GetType (), "ctrScript" , "<script language = 'javascript'>Lo adControls('" & strParams & "')</script>")

      Thanks

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        I'm not sure about ASP.NET, but in JavaScript you can use window.onload or event listeners, e.g.
        Code:
        if (window.addEventListener)
            window.addEventListener("load", LoadControls, false);
        else if (window.attachEvent)
            window.attachEvent("onload", LoadControls);
        else 
            window.onload= LoadControls;

        Comment

        • omlac
          New Member
          • Aug 2008
          • 10

          #5
          thanks,
          my function loadcontrols has a parameter strParams which is generated in code behind(either onload or on init). do u thinki will be able to pass this parameter if i useevent listners.

          Thanks

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Use a function() object:
            Code:
            if (window.addEventListener)
                window.addEventListener("load", function() { LoadControls(params); }, false);
            else if (window.attachEvent)
                window.attachEvent("onload", function() { LoadControls(params); });
            else 
                window.onload= function() { LoadControls(params); };

            Comment

            • omlac
              New Member
              • Aug 2008
              • 10

              #7
              thanks, it worked, that fixed it

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                That's cool. Thanks for posting.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  I have no idea why you are doing what you are doing....

                  I just wanted to mention that if you plan on using Ajax in your asp web application you should register your scripts with the ScriptManager instead of the Page.ClientScri pt....

                  I'm not even sure your code would work in an Ajax solution...so don't mind me.

                  Comment

                  Working...