Disabling .NET Controls Via JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    Disabling .NET Controls Via JavaScript

    I've run into a bit of a problem concerning JavaScript and ASP.NET.

    Upon page submission I disable all of my buttons and would also like to disable all of my inputs (Textboxes, DropDownLists, etc) as well. This will prevent multiple postbacks to the server while it's processing and keep the user from making changes during this time.

    The problem is that when I use JavaScript to disable the input controls the values of these objects come back as nothing.

    The JavaScript is quite simple:
    [code=JavaScript]
    function DisableAllInput s()
    { var i;

    var allInputElement s=document.getE lementsByTagNam e('input');
    var len=allInputEle ments.length;
    for(i = 0; i < len; i++)
    {
    var temp = allInputElement s[i];
    if(temp.type==" text")
    { temp.disabled=t rue;
    }
    }

    var allDropDownList s=document.getE lementsByTagNam e('select');
    var lenOfDDLs=allDr opDownLists.len gth;
    for(i = 0; i < lenOfDDLs; i++)
    { var temp = allDropDownList s[i];
    temp.disabled=t rue;
    }
    }[/code]

    Does anyone have an idea of why the values of the TextBoxes and DropDownLists are nothing when the page is posted back to the server and JavaScript has been used disabled these controls?

    -Frinny
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I found the solution to my problem.

    The reason why the controls had a value of nothing upon posting back to the server was because when you use JavaScript to disable the control it is removed from the Request.Form collection.

    To get around the problem I used a <div> to place a "sheild" over top of the page's content:

    [code=asp]
    <div id="shield" style="display: none; position:absolu te; top:0; left:0; height:100%; width:100%; background-color:black; filter:alpha(op acity=35); opacity:.35;">& nbsp;</div>

    <div id="maincontent ">
    <!-- the content of the page goes here-->
    </div>
    [/code]

    Now, when the page is submitted I call the following JavaScript function. It displays the "shield" over top of the entire page.

    [code=JavaScript]
    function DisplaySheild()
    {
    document.getEle mentByID("sheil d").style.displ ay = 'block';
    }[/code]

    Note that the style of the "shield" <div> has "position:absol ute", this is how the "shield" is displayed "on top" of the page content. Also note that I have the background colour of the "sheild" set and I am using opacity to allow the content under it to show through, you don't need to do this if you don't want to.

    Hope this helps you!

    -Frinny

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Update: The <form> tag in asp.net has a property SubmitDisabledC ontrols.

      If you set: submitdisabledc ontrols="True" then the controls disabled by the JavaScript code above will keep their values.

      I'm sticking with my last solution because it disables link buttons etc. while the page is posting back.


      Cheers!

      -Frinny

      Comment

      Working...