AsynFileUploadedComplete函数可以';t启用按钮控件

本文关键字:启用 按钮 控件 函数 AsynFileUploadedComplete | 更新日期: 2023-09-27 18:30:01

主页面表单标签:

    <form runat="server" enctype="multipart/form-data" method="post">

FileUpload.aspx

 <asp:UpdatePanel runat="server" ID="upnlFileUpload">     
  <ContentTemplate> 
  <asp:Button ID="Save" runat="server" Text="Save" 
        style="margin-left: 0px" Width="350px" onclick="Save_Click" />
    <ajaxToolkit:AsyncFileUpload
        ID="AsyncFileUpload1" runat="server" 
        onuploadedcomplete="AsyncFileUpload1_UploadedComplete" />
  </ContentTemplate>
  </asp:UpdatePanel>    

FileUpload.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           btnSave.Enabled = false;
        }
    }

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        btnSave.Enabled = true;
    }

如果我上传一个文件,UploadedComplete实际上会被触发(我通过将文件保存在临时位置来测试它),但"保存"按钮没有被启用。即使是断点也能击中线路:

btnSave.Enabled = true;

这个代码出了什么问题?!

AsynFileUploadedComplete函数可以';t启用按钮控件

您需要做一些调整。

以下是您需要做的使按钮禁用:

在您的<head>标签中:

<script type = "text/javascript">
        function ClientUploadComplete(sender, e) {
            var btnSave = document.getElementById("<%=btnSave.ClientID %>");
            btnSave.disabled = true;
            btnSave.value = "Save";
        }
</script>

在您的<body>标签中:

先添加<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>,然后添加

 <asp:UpdatePanel runat="server" ID="upnlFileUpload">     
  <ContentTemplate> 
   <asp:AsyncFileUpload ID = "AsyncFileUpload1" runat = "server" OnClientUploadStarted = "" OnClientUploadComplete = "ClientUploadComplete" />
    <asp:Button ID="btnSave" runat="server" Text="Save" />
  </ContentTemplate>
  </asp:UpdatePanel>