1

I have a dropdownlist and a fileupload control in a panel which is added with other controls in an UpdatePanel. When a value is selected from the dropdown and a file is uploaded, the Upload button is enabled. For this I have a javascript function as follows:

function SetUploadButtonEnabled(controlPanel, fileUploadId) {
     var docTypesList = controlPanel.find("select");
     var gotListVal = docTypesList.val() != "";
     var fileUpload = $find(fileUploadId);
     var gotFileVal = fileUpload.getUploadedFiles().length > 0;
     var enable = gotListVal && gotFileVal;
     if (enable) {
         controlPanel.find(".GxButtonBlue").removeAttr("disabled");
     }
     else {
         controlPanel.find(".GxButtonBlue").attr("disabled", true);
     }
 }

I am trying to call it from code behind as follows, but the function is not being called:

string script = "<script type=\"text/javascript\">"
                + "\n  $(document).ready(function (){"
                    + "\n    $(document).on(\"change\", \"#" + this._DdDocumentTypes.ClientID + "\", function(event){"
                    + "\n      var docUploadControlPanel = $(this).closest(\"#" + this._DocUploadControlPanel.ClientID + "\");"
                    + "\n      SetUploadButtonEnabled(docUploadControlPanel, \"" + this._fiInput.ClientID + "\");"
                    + "\n    });"
                    + "\n  });"
                    + "\n "
                    + "</script>";

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DocumentAjaxUploadCtrlScript_" + this.ClientID, script);

Since an Update Panel is there I even tried:

  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "DocumentAjaxUploadCtrlScript_" + this.ClientID, script, true);

Please help me find why the function is never called!

R B
  • 413
  • 3
  • 8
  • 19
  • 2
    Have you tried moving the dynamically generated JavaScript from the code-behind to the UI? I understand the problem of ClientID. You can inject those to the jQuery. If you'd like an example, I can provide one. The problem is that your script won't get executed when the form performs an AJAX postback. It will only get executed if you do a "hard" postback (i.e. without the UpdatePanel). – Mario J Vargas Jun 03 '14 at 20:03
  • This should be helpful. With update panels, you can't rely on document ready like you can normally, basically because an update panel works nothing like a regular postback / page load. http://stackoverflow.com/questions/256195/jquery-document-ready-and-updatepanels – p e p Jun 03 '14 at 20:11
  • @Mario J Vargas Please provide me an example. Thanks – R B Jun 03 '14 at 20:48

1 Answers1

0

Here's one way to do it. The key here is the pageComponents object.

ASPX or User Control

<script>
var pageComponents = {
    documentTypeSelector: "#<%= _DdDocumentTypes.ClientID %>",
    uploadControlSelector: "#<%= _DocUploadControlPanel.ClientID %>",
    fiInputSelector: "#<%= _fiInput.ClientID %>"
};
</script>

JavaScript Place after the above

function SetUploadButtonEnabled(controlPanel, fileUploadId) {
     var docTypesList = controlPanel.find("select");
     var gotListVal = docTypesList.val() != "";
     var fileUpload = $find(fileUploadId);
     var gotFileVal = fileUpload.getUploadedFiles().length > 0;
     var enable = gotListVal && gotFileVal;
     if (enable) {
         controlPanel.find(".GxButtonBlue").removeAttr("disabled");
     }
     else {
         controlPanel.find(".GxButtonBlue").attr("disabled", true);
     }
 }

$(document).ready(function (){
    $(document).on("change", pageComponents.documentTypeSelector, function(event){
        var docUploadControlPanel = $(this).closest(pageComponents.uploadControlSelector);
        SetUploadButtonEnabled(docUploadControlPanel, pageComponents.fiInputSelector);
    });
});

Remarks

You can avoid using the "bee sting" syntax above by setting the control ClientIDMode property to Static (assuming you're using only ASP.NET Page, not a user control. Then your JavaScript would look like below:

$(document).ready(function (){
    $(document).on("change", "#documentType", function(event){
        var docUploadControlPanel = $(this).closest("#uploadControl");
        SetUploadButtonEnabled(docUploadControlPanel, "#fiInput");
    });
});

In addition the line:

var docUploadControlPanel = $(this).closest(pageComponents.uploadControlSelector);

could be written as:

var docUploadControlPanel = $(pageComponents.uploadControlSelector);

since ClientID always returns a unique element ID for the entire page.

Mario J Vargas
  • 1,185
  • 6
  • 12