简体   繁体   中英

AjaxFileUpload does not work in ASP.NET VB.Net

AjaxFileUpload works on visible TabContainer control TabPanels yet not on ones that are initially invisible and then set to visible.

I believe the issue would be resolved if the visibility property of the TabPanels is set by JavaScript rather than from the server but doesn't know how to do it. Please help me to fix this issue. Thanks.

ASPX Code:


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AjaxFileUpload.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">
    
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true"></asp:ToolkitScriptManager>

    <p>AjaxFileUpload works on visible TabContainer control TabPanels yet not on ones that are initially invisible and then set to visible.</p>
    <p>I believe the issue would be resolved if the visibility property of the TabPanels is set by JavaScript rather than from the server.</p>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

    <ContentTemplate>

        <asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">

            <asp:TabPanel ID="TabPanel1" runat="server" HeaderText="TabPanel 1">

            <ContentTemplate>

            <asp:Button ID="btnShow" runat="server" Text="Show"></asp:Button>

            </ContentTemplate>

            </asp:TabPanel>

            <asp:TabPanel ID="TabPanel2" runat="server" HeaderText="TabPanel 2" Visible="false">

            <ContentTemplate>
            
            <asp:Button ID="btnHide" runat="server" Text="Hide"></asp:Button>

<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" AllowedFileTypes="txt,xls,xlsx,doc,docx,msg,pdf,bmp,gif,jpg,jpeg,png" MaximumNumberOfFiles="5" Width="500px" OnUploadComplete="AjaxFileUpload1_OnUploadComplete"></asp:AjaxFileUpload>
  
            </ContentTemplate>

            </asp:TabPanel>

        </asp:TabContainer>    

    </ContentTemplate>

    </asp:UpdatePanel>
    
    </form>

</body>

</html>


Backend VB.Net Code:


Imports System.IO
Imports AjaxControlToolkit

Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub btnShow_Click(sender As Object, e As System.EventArgs) Handles btnShow.Click

        If TabPanel2.Visible = False Then TabPanel2.Visible = True
        TabContainer1.ActiveTabIndex = 1
        AjaxFileUpload1.Visible = True

    End Sub

    Protected Sub btnHide_Click(sender As Object, e As System.EventArgs) Handles btnHide.Click

        If TabPanel2.Visible = True Then TabPanel2.Visible = False

    End Sub

    Protected Sub AjaxFileUpload1_OnUploadComplete(ByVal sender As Object, ByVal e As AjaxFileUploadEventArgs)

        Dim strPath As String = Server.MapPath("~/Uploads")
        If Not Directory.Exists(strPath) Then Directory.CreateDirectory(strPath)

        Dim sFilename As String = System.IO.Path.GetFileName(e.FileName)
        Dim sUploadPath As String = "~/Uploads/"
        AjaxFileUpload1.SaveAs(Server.MapPath(sUploadPath) + sFilename)

        Dim filePath As String = Server.MapPath("~/Uploads/" & e.FileName)
        Dim filename As String = Path.GetFileName(filePath)

    End Sub

End Class



Actually, even if you set a textbox with visible=false, then the control is NOT sent down to the web page. So, you can't client side even turn that simple text box to become visible.

Use style, and display none.

The page first time is initialized, and that includes js wriing up the ajax up-loader.

So, put the mess in a div (with the control, and anything else you need to hide/show), say like this:

 <div id="uploader" runat="server" style="display:none;width:50%">

     <ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" 
          ChunkSize="8192" 
          ClientIDMode="Static"
          ViewStateMode="Enabled" />
 </div>

So, if you are in SERVER side code (some button postback), then you can show the "div" like this:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Me.uploader.Style("display") = "inline"

End Sub

and to hide:

    Me.uploader.Style("display") = "none"

Remember, while we are in a post back, all that "jazz" JavaScript STILL needed to do it dance on the FIRST page load. (so, we can't use visible = false) on first page load (and against the control or div). You need the full page to load - including the truckload of code that ONLY runs the FIRST time the ajaxupload control loads. If it not a first page load - the control will not setup and initialize correctly.

So, by using style, then the control is rendered when the page is first loaded. And this concept not only applies to the ajax, but say for a hidden text box or what not? Again, using style with display none.

In fact, DURING the process of a user selecting files, and up-loading with that control? Well, you don't want a post back. But when the whole up-load is done? And the server side events (such as saving the files), then I often DO WANT some final code, or even the web page to move on. I do want a final post-back to move on. But, I need a post-back client side!

In place of using a JaveScript _dopostback? (which forces you to write parameter code in the server side page load event? I just drop a asp button on the form some place, and HIDE it (of course with style="display:none").

Now I can use client side js to to "click" of that hidden button. The beauty is then the server side event code stub for the single button code runs, and I don't have write up event code with _doPostBack + code in load event for a code stub server side to run!

Now, in the above, I displayed the up-loader using a server side code-behind.

But, you can ALSO rather easy have client side code to hide or show that div.

If you using jQuery()?

Well, jQuery has.hide(), and.show() (and guess what.., -.hide()/.show() actually sets the style for you!!! - so even jQuery assumes that you hide or show controls with style, since using the "SomeControl.visible" is near use-less (since anything set visible = false is NOT rendered client side - so then you can't hide/show anyway!!!).

So, to hide/show that with jQuery, you could use this client side:

         function show() {
             $('#uploader').show();
         }

         function hide() {
             $('#uploader').hide();

         }

And, if you not using jQuery, and want to hide/show the div? (javascript).

This will work:

         function showl2() {
             var mydiv2 = document.getElementById('<%=uploader.ClientID%>');
             mydiv2.style.display = "inline";
         }

         function hidel2() {
             var mydiv = document.getElementById('<%=uploader.ClientID%>');
             mydiv.style.display = "none";
         }

So, the simple concept here is that the control has to render on the page load and must do the first time. So, it has to come down to the browser side. Any control not visible will not be sent down to the client side. However, using css means the control is sent down client side - in all cases.

Now, after a upload, and I want a full post back of the page? Well, as note, this can be difficult, since after the ajax server side events run (such as saving the file), you do NOT get a final post back when all is said and done.

So, I set a client side event for the ajax up-loader like this:

 <ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" 
      AllowedFileTypes="pdf,zip"
      ChunkSize="8192" 
      OnClientUploadCompleteAll="UpLoadDone"
  />

So, note the above client side event - all code (both server and client is done). But we now want a postback from the client side.

So, in the UpLoadDone routine?

I have this:

  function uploaddone() {
    document.getElementById('<%= btnProof.ClientID %>').click();
    }

So, I get a post back and ALSO the code in the button stub server side also runs. And that btProof is hidden with style. But, it is a regular asp.net button. I just wanted it to be clicked after the ajax file up-load is done. But, there not really a way to get the ajax up-load to fire a postback when all is said and one. But, once again: The concept of a hidden control with style is used - since if I hide that button with.visible = false, then in fact the button would not exist client side, and never be rendered or placed into the browser client side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM