Javascript change to disabled ASP control lost after postback


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Javascript change to disabled ASP control lost after postback

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    4

    Javascript change to disabled ASP control lost after postback

    Hi,
    I'm trying to learn web form design using Visual Studio, Visual Basic.NET, ASP.NET and javascript. I've noticed some odd behavior when I change disabled ASP form controls using javascript functions. I'm trying to dynamically alter the controls on the form depending on input from the user by using client-side javascript rather than requiring postbacks and VB code on the server. However, when I change a disabled control using javascript, it reverts back to the prior state after the form is submitted and posted back(postbacked?). All my ASP controls have runat=”server”.

    Here’s the simplest example of the problem:

    The form includes two ASP text boxes, txtBirthDate and txtAge, where txtAge is disabled and set=”xx” in the initial pass through Page_Load. When the user enters a date in txtBirthDate and tabs to next field, I have OnBlur event linked to a javascript function that computes age and puts the value in txtAge. This works fine. When the form is submitted or some other control triggers a postback, I do not reset or clear any of the fields but for some reason the txtAge field reverts to “xx”. This example is trivial but there are other cases where controls toggle between disabled and enabled and the setting changes after submit/postback. Those cases are more annoying.

    The problem with txtAge reverting to previous value can be eliminated by setting txtAge to enabled. Then when my javascript function puts the age value in the txtAge box, the value remains after submit/postback. Also, if I use autopostback and set the age value in a txtBirthDate_TextChanged event, the value remains after submit. But, I would rather not do that.

    I can sort of accept the fact that maybe javascript execution might not be recognized by ASP/VB and the values would get reset after each postback. However, I’m very confused by the fact that this problem only occurs with DISABLED controls and not with enabled ones.

    Any suggestions?

    Thanks,
    Pat G.

  2. #2
    Join Date
    Mar 2005
    Posts
    4
    I should describe the commands I’m using. In the initial pass through the VB Page_Load event I have :
    txtAge.Enabled = "False"
    txtAge.Text = “xx”

    In the javascript function I have:
    document.getElementById('txtAge').value = intAge.toString();

  3. #3
    Join Date
    Mar 2005
    Posts
    4

    Javascript change to disabled ASP control lost after postback

    I've made some progress solving this problem and the key seems to be handling the changes both in javascript(client) and VB(server) in Page_Load for example. One tip I got on another forum was to leave the control enabled but set readonly property, something I was unfamiliar with. This serves the purpose of making the field un-editable but retains value through postbacks. Setting the tabindex to -1 takes it out of the tab sequence then. - Pat G.

  4. #4
    Join Date
    May 2006
    Posts
    2

    I too am dealing with this issue

    In your case the issue may be worked around by using the readOnly property but once you venture outside of textbox controls you will run into the problem I'm dealing with. The readOnly property doesn't apply to all HTML elements so it cannot be used as a pseudo disable. I have a situation in which I have a listbox control I need to disable when the user submits the form. The problem is the listbox contains a list of items the user has selected to be processed by the form so when my .Net code begins to execute after the form is posted back, it finds no items in the listbox have been selected.

    I've considered reworking the form using AJAX since it really should have been designed that way in the first place anyway but this is one of those annoying issues that I would like to find a solution to prior to moving on. I suspect that if I just work around it now I'll only find myself faced with the same issue in the future.

  5. #5
    Join Date
    Aug 2005
    Posts
    6
    One thing you can do is check the Page.IsPostback in the Page_Load sub. If the page is being posted (ie. not the original load) then don't set the text field to disabled and = "xx".

  6. #6
    Join Date
    May 2006
    Posts
    2
    Or in my case, I'm moving to .NET 2.0 which will give me the capability of posting back controls/elements that are disabled. Of course not everyone can simply rework their application and move from 1.1 to 2.0 .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center