AsyncFileUpload事件未被触发

时间:2014-04-11 07:58:51

标签: asp.net events file-upload ajaxcontroltoolkit

我有以下鳕鱼:

<asp:AsyncFileUpload ID="asyncUploader" runat="server" ThrobberID="myThrobber" BackColor="#ff99cc"/><br /><br />
<asp:Label runat="server" ID="debug"></asp:Label>

c#文件看起来很喜欢这个:

 protected void Page_Load(object sender, EventArgs e)
 {
        asyncUploader.UploadedComplete += new EventHandler<AsyncFileUploadEventArgs>(uploadComplete);
 }

 protected void uploadComplete(object sender, AsyncFileUploadEventArgs e)
 {
        debug.Text += "y";
        if (asyncUploader.HasFile == true)
        {
            debug.Text += "x";
        }
 }

当我选择要上传的文件或完成下载时,标签中的文本不会更改。我做错了吗?

1 个答案:

答案 0 :(得分:0)

标签文本实际上会发生变化,但由于AsyncFileUpload 回发,每次调用uploadComplete方法时都会重置。所以更好的方法是使用javascript来更改标签文本。

您可以使用AsyncFileUpload的OnClientUploadComplete事件来完成此任务。

OnClientUploadComplete事件添加到AsyncFileUpload标记中,如下所示:

<asp:AsyncFileUpload OnClientUploadComplete="uploadComplete" ID="asyncUploader" runat="server" ThrobberID="myThrobber" BackColor="#ff99cc"/><br /><br />
<asp:Label runat="server" ID="debug"></asp:Label>

然后在标记中添加uploadComplete函数:

   <script type="text/javascript">
        function uploadComplete(sender, args) {
            //First, change the label text
            document.getElementById("<%= debug.ClientID %>").innerHTML += "y";
            //Check if the upload has files or not
            if (document.getElementById("asyncUploader").value != "") {
                //It has files, change the label text
                document.getElementById("<%= debug.ClientID %>").innerHTML += "x";
            }
        }
    </script>

这样你就不需要在代码隐藏中做任何事情,所有这些都将在javascript中完成。

希望这有帮助。