AsynFileUpload UploadedComplete函数无法启用Button控件

时间:2012-04-05 10:46:49

标签: c# asp.net

母版页表单标记:

    <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;

这段代码有什么问题?!

1 个答案:

答案 0 :(得分:1)

你需要做一些调整。

以下是禁用按钮所需执行的操作:

在你<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>