AjaxFileUpload呈现带有事件的黑色按钮

时间:2014-06-25 10:55:27

标签: c# asp.net ajaxcontroltoolkit

到目前为止,我花了一整天的时间。我正在尝试使用Ajax Control Toolkit中的FileUpload控件。只要我向控件添加一个EventHandler:

OnClientUploadComplete="File_Upload"

它只呈现一个黑色按钮。我已经查看了有关此问题的所有问题。我正在使用framwork 4.0并仔细检查我使用的是4.0版本的工具包。我在Visual Studio项目中引用了该工具包。这是我的.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebUpload_de.aspx.cs" Inherits="WebFileTransfer.WebUpload_de" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxControlToolkit" %>
<!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">
    <link rel="stylesheet" href="Default.css" type="text/css" media="screen" />
    <title>File Upload</title>
</head>
<body>
    <form id="form1" runat="server">
        <AjaxControlToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" 
            runat="server">
        </AjaxControlToolkit:ToolkitScriptManager>
        <div>
            <AjaxControlToolkit:AjaxFileUpload ID="AjaxFileUpload1" 
                runat="server" OnClientUploadComplete="File_Upload"
                Width="500px" />
        </div>
    </form>
</body>
</html>

到我的web.config我添加了这个:

<system.web>
    <httpHandlers >
      <add verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler, AjaxControlToolkit"/>
    </httpHandlers>
</system.web>

<system.webServer>    
    <handlers>
      <add name="AjaxFileUploadHandler" verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler, AjaxControlToolkit"/>
    </handlers>
  </system.webServer>

后面的代码定义了函数:

protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
    {
        string filename = e.FileName;
        string strDestPath = Server.MapPath("~/Documents/");
        AjaxFileUpload1.SaveAs(@strDestPath + filename);
    }

当我从AjaxFileUpload标记中删除OnClientUploadComplete时,控件才会呈现。

有人可以解释一下吗?

1 个答案:

答案 0 :(得分:0)

OnClientUploadComplete是客户端事件处理程序。它必须绑定到JavaScript函数。只需使用此功能添加到您的页面script元素:

function File_Upload(sender, args){
  alert("File uploaded");
}