0

I'm after what seems to me to be a straightforward pattern for handling page refreshes when I've got a drop-down that reacts to the onchange event.

I've used the following in the vb code behind (in the Load handler):

MyDropDown.Attributes.Add("onchange", "ProcessDDChange(this.value);")

Function ProcessDDChange() is in-page JavaScript that grays out some other form inputs for certain values of the drop down.

This works fine, but after a postback, onchange is apparently not fired when the previous state is restored, so disabled boxes are enabled again.

I've investigated load events (page and drop-down), but both fire too early to be of use and I can't see any later options.

Is there a standard way of doing this? I need a hook for running a js function post DOM setup, post asp state restore.

Info

I'm using .net 3.5 and I'm looking for a cross browser solution. This is not my project, so I can't add jQuery (much as I'd like to) or other libs.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Bob Sammers
  • 3,080
  • 27
  • 33

2 Answers2

1

onchange does not fire when the page loads. You would need to trigger the function on page load OR you need to have the server set up the page correctly from the start.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thanks for your reply. I sort of understood all this when I asked the question. I'm really looking for pointers to (hopefully standard) techniques to actually do what you suggest. I'm something of a newbie when it comes to asp. – Bob Sammers Jan 23 '15 at 19:00
  • RegisterStartUpScript and trigger the change event on the element. http://stackoverflow.com/questions/2856513/how-can-i-trigger-an-onchange-event-manually – epascarello Jan 23 '15 at 19:07
  • I think I see where you're going, but the script registered by `RegisterStartupScript` [is run before](https://msdn.microsoft.com/en-us/library/asz8zsxy%28v=vs.110%29.aspx) `onload` is raised, which is itself too early to process the restored state data. – Bob Sammers Jan 23 '15 at 19:49
1

You could wrap the dropdown in an update panel and set the trigger for the change event like epascarello suggests. Have you tried adding it to the markup like

 <asp:DropDownList runat="server" ID="MyDropDown" onchange="ProcessDDChange(this.value);"></asp:DropDownList>

EDIT:

So you are loosing the onchange listener when you postback, the above example should preserve it. But if not you could also try this in the codebehind event that you would like to have call the ProcessDDChange: for vb.net

  ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "err", "ProcessDDChange(this.value);", true);

FINAL EDIT:

Thanks for sticking it out with me Bob, while I was attempting to understand the question: The document.ready event is raised after the PageLoad event is complete and the DOM is constructed. This would be the proper place to call your ProcessDDChange().

  document.addEventListener("DOMContentLoaded", function(event) { 
      ProcessDDChange(ddlId.selectedValue);
    });
Aerokneeus
  • 119
  • 5
  • Thanks for taking the time to reply. I don't have a problem adding the `onchange` event to the control. My problem is that I need to fire the same js method after a postback (and after the DOM is constructed and asp has restored the saved state). – Bob Sammers Jan 23 '15 at 19:41
  • No, I'm not losing the event listener. Sorry if I gave you that impression. I'm losing the `disabled` attribute on certain other input fields, so I want to call the `ProcessDDChange()` function after a postback page load has finished (which will reinstate them, if necessary). I'm wondering if the only way to do it is to duplicate the function in the code behind and insert the disabled attributes during server processing of a postback. – Bob Sammers Jan 23 '15 at 19:55
  • 1
    Ah ok, then you need to call ProcessDDChange in the window.onload or document.ready event. – Aerokneeus Jan 23 '15 at 20:00
  • Thanks. I've used the `document.ready` event, which I'd somehow failed to see earlier and it's working fine! If you change your answer to reflect that this event is raised after the DOM is constructed and the asp state is restored, that should answer my original question and I'll be happy to accept it. – Bob Sammers Jan 23 '15 at 20:34