是否可以在Jquery中使用bind与非标准客户端事件

时间:2012-07-31 10:06:49

标签: jquery asp.net events ajaxcontroltoolkit unobtrusive-javascript

例如这个AsyncFileUpload,有一个事件“OnClientUploadComplete”

我能做点什么吗

$("#ctl00_MainContent_AsyncFileUpload1_ctl02")
.bind("OnClientUploadComplete", function () {  alert("test"); 
 })
<ajaxToolkit:AsyncFileUpload  OnClientUploadComplete="uploadComplete" 
    runat="server" ID="upload"
Width="400px" UploaderStyle="Modern" CompleteBackColor="White" UploadingBackColor="#CCFFFF"
ThrobberID="imgLoader" OnUploadedComplete="AsyncFileUpload1_UploadedComplete" 
    OnClientUploadStarted = "uploadStarted" />
 <asp:Image ID="imgLoader" runat="server" ImageUrl="~/images/loader.gif" /><br /><br />
 <img id = "imgDisplay" alt="" src="" style = "display:none"/>
     

$addHandler($("#<%=upload.ClientID %>"), 'uploadComplete', (function () { alert("test"); }));


function uploadStarted() {
    $get("imgDisplay").style.display = "none";
}
function uploadComplete(sender, args) {
    var imgDisplay = $get("imgDisplay");
    imgDisplay.src = "images/loader.gif";
    imgDisplay.style.cssText = "";
    var img = new Image();
    img.onload = function () {
        imgDisplay.style.cssText = "height:100px;width:100px";
        imgDisplay.src = img.src;
    };
    img.src = "<%=ResolveUrl(UploadFolderPath) %>" + args.get_fileName();
} </script>

1 个答案:

答案 0 :(得分:0)

不,你不能这样做。

AJAX Control Toolkit中的自定义事件在浏览器的事件系统之外实现。有关详细信息,请参阅here

您必须使用组件提供的add_uploadComplete()方法:

$find("yourBehaviorID").add_uploadComplete(function() {
    alert("test").
});

其中yourBehaviorID是您的扩展程序的BehaviorID(默认为扩展控件的ClientID)。