title: Button not firing event of first click causing strange results with FileUpload control in IE (ASP.NET)
body: <p>I came across a strange situation with a button and the fileupload control in IE only. I did find a temporary way around this issue, however i would like to find out what called be the actual cause of the issue and if there is a better way around it then what i have done.</p>
  
  <p>There a few things to note about the application.
  1. I am working with a masterpage
  2. I have an updatepanel on the masterpage
  3. Controls in question are on a clientPage
  4. I have hidden the fileupload control and instead show users a "custom" file upload box within a modalpopup. Fileupload control is not on the modalpopup
  5. The following works in all browsers except IE</p>
  
  <p>Please note this has nothing to do with fileupload not working with updatepanels, i have already added the button used to upload the file as a trigger to the updatepanel. </p>
  
  <p><strong>How code works:</strong>
  A user clicks an image which causes the hidden fileupload to trigger and open browse file dialog box. Once user has selected the file, a modalpopup appears with a textbox and two buttons (browse and upload). The textbox has the file name, the browse button is used to open the browse file dialog box again, and the upload button is used to uplood the file. </p>
  
  <p>This all works perfectly in all browers except IE. In IE the following happens:
  When user clicks the upload button it appears as nothing has happened, if they click the button again it then fires the button event, but then the fileupload control states that is has no file. </p>
  
  <p>After i unhide the fileupload control I found that the following was happening:
  On first button click the fileupload was being cleared, and not doing a postback.
  Second button click the postback occured but fileupload is now empty resulting in my control not having any file.</p>
  
  <p>To get around this i did the following. On the button's first click i fire a clientside event and using jquery do the following if the browser is IE:
  Take the full filename and path of the file from the fileupload control and set it as the value for a hidden field (possible due to IE not hiding filepath like other browsers). Then force it to fire the click event for another button, that goes to a different onclick method to upload the file.</p>
  
  <p><strong>Code:</strong></p>
  
  <p><strong>image</strong></p>
  
  <pre><code>&lt;a onclick="uploadKycSubType('Identity','','0')"&gt;
  &lt;img src="../images/upload.png"&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p><strong>modal popup and fileupload control:</strong></p>
  
  <pre><code>&lt;cc1:ModalPopupExtender ID="ModalPopupExtender4" runat="server" TargetControlID="hdnTarget4" PopupControlID="pnlUpload" BackgroundCssClass="modalBackground"&gt;
  &lt;/cc1:ModalPopupExtender&gt;
  
  &lt;cc1:AnimationExtender ID="AnimationExtender4" runat="server" TargetControlID="btnUploadClose"&gt;
      &lt;Animations&gt;
              &lt;OnClick&gt;
                  &lt;Sequence AnimationTarget="pnlUpload"&gt;
                      &lt;Parallel AnimationTarget="pnlUpload" Duration=".7" Fps="20"&gt;
                          &lt;Move Horizontal="200" Vertical="200"&gt;&lt;/Move&gt;
                          &lt;Scale ScaleFactor="0.05" FontUnit="px" /&gt;
                          &lt;FadeOut /&gt;
                      &lt;/Parallel&gt;
                      &lt;StyleAction Attribute="display" Value="none"/&gt;
                      &lt;StyleAction Attribute="height" Value=""/&gt;
                      &lt;StyleAction Attribute="width" Value=""/&gt;
                      &lt;StyleAction Attribute="fontSize" Value="14px"/&gt;
                      &lt;EnableAction AnimationTarget="btnUploadClose" Enabled="true" /&gt;
                  &lt;/Sequence&gt;
              &lt;/OnClick&gt;
      &lt;/Animations&gt;
  &lt;/cc1:AnimationExtender&gt;
  
  &lt;asp:FileUpload ID="imageUpload" CssClass="fileInput" runat="server" onchange="$('#hiddenText').val($(this).val().split('\\').pop());uploadChange();" /&gt;
  
  &lt;asp:Panel ID="pnlUpload" runat="server" CssClass="Modal_Upload" &gt;
      &lt;table width="100%" style="border-collapse:collapse"&gt;           
          &lt;tr&gt;
              &lt;td colspan="2"&gt;
                  &lt;h2&gt;&lt;%#bindValue("UploadDocument")%&gt;&lt;/h2&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr &gt;
              &lt;td colspan="2"&gt;
                 &lt;p&gt;&lt;%#bindValue("UploadDocumentInfo")%&gt;.&lt;/p&gt; 
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;div class="hide"&gt;
                     &lt;input type="text" class="textbox" id="hiddenText"/&gt;
                  &lt;/div&gt;
                  &lt;input type="text" class="textbox" id="imageText"  style="width:350px"/&gt;    
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;input type="button" value="&lt;%#bindValue("Browse")%&gt;" onclick="browse()" class="buttonClose" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;div class="AddSpace"&gt;
                  &lt;/div&gt;
                  &lt;asp:Button runat="server" ID="btnUploadClose" CssClass="buttonClose" OnClick="btnClose_Click"/&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;div class="AddSpace"&gt;
                  &lt;/div&gt;
                  &lt;asp:Button runat="server" ID="btnUpload" CssClass="buttonShort" OnClick="btnKycUpload_Click" OnClientClick="uploadIEKyc();"/&gt;
              &lt;/td&gt;
              &lt;asp:Button runat="server" ID="Button1" CssClass="fileInput" OnClick="btnKycUploadIE_Click"/&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/asp:Panel&gt;
  </code></pre>
  
  <p><strong>jquery functions</strong></p>
  
  <pre><code>function uploadKycSubType(link,bankAccount,bankNumber) {
  
          $("#&lt;%= hdnEdit.ClientID %&gt;").val(link);
          $("#&lt;%= hdnBank.ClientID %&gt;").val(bankAccount);
          $("#&lt;%= hdnBankNumber.ClientID %&gt;").val(bankNumber);
  
          browse();
      }
  
      function browse(){
          $('#&lt;%= imageUpload.ClientID %&gt;').click();
      }
  
      function uploadChange() {
          $('#imageText').val($('#hiddenText').val());
  
          if($("#&lt;%=pnlUpload.ClientID %&gt;").css("display") == "none") 
              $find("MainContent_ModalPopupExtender4").show();
      }
  
      function uploadIEKyc() {
          var ua = $.browser;
  
          if (ua.msie) {
              $('#&lt;%= hdnFile.ClientID %&gt;').val($('#&lt;%= imageUpload.ClientID %&gt;').val());
              $('#&lt;%=Button1.ClientID %&gt;').click();
          }
      }
  </code></pre>
  
  <p><strong>Postbehind</strong></p>
  
  <p>Add button to trigger</p>
  
  <pre><code>protected void Page_Init(object sender, EventArgs e)
      {
          PostBackTrigger trigger = new PostBackTrigger();
          trigger.ControlID = btnUpload.UniqueID;
          UpdatePanel up = (UpdatePanel)Page.Master.FindControl("Updatepanel1");
          up.Triggers.Add(trigger);
      }
  </code></pre>
  
  <p>uploadfile click events </p>
  
  <pre><code> protected void btnKycUpload_Click(object sender, EventArgs e)
      {
  
          if (imageUpload.HasFile)
          {
              //do file upload
          }
          else
          {
              this.ShowInstruction(ErrorName.DocumentNotSaved, Session["username"] == null ? string.Empty : Session["username"].ToString());
              ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ScrollTo", "topScreen();", true);
          }
  
      }
  
        protected void btnKycUploadIE_Click(object sender, EventArgs e)
      {
          string files = hdnFile.Value;
  
          if (!string.IsNullOrEmpty(files))
          {
              FileInfo fi = new FileInfo(files);
  
              string fileName = fi.Name;
              string fileExtension = fi.Extension;
              fileExtension = fileExtension.Substring(fileExtension.IndexOf(".") + 1);
  
              //do file upload
  
          }
          else
          {
              this.ShowInstruction(ErrorName.DocumentNotSaved, Session["username"] == null ? string.Empty : Session["username"].ToString());
              ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ScrollTo", "topScreen();", true);
          }
      }
  </code></pre>
  
  <p>If you require any thing else in regards to this, let me know, other wise thank you for your time and help.</p>