【问题标题】:Callback function?回调函数?
【发布时间】:2010-10-14 18:40:48
【问题描述】:

我需要在我的代码中回调 Javascript 函数,但不触发。我正在提供我在做什么的详细信息?

  1. 我在调用 javascript 函数的页面中有输入按钮。我正在加载另一个 ProfilePic.aspx 页面。 ProfilePic.aspx 有 FileUpload、OK 和取消按钮

    <input type=button value="Change Image" onclick="javascript:SelectUserImage()" />
    
  2. Javascript 函数是

    <script type="text/javascript">
    function SelectUserImageCallback(ret) {
        var imgId = 'ctl00_PlaceHolderMain_prof_imgUser';
        var clearId = 'ctl00_PlaceHolderMain_prof_hidImageURL';
        if (ret) {
            if (ret == '__RESET__') {
                document.getElementById(imgId).src = '\u002f_layouts\u002fimages\u002fno_pic.gif';
                document.getElementById('ctl00_PlaceHolderMain_prof_hidImageURL').value = '';
                document.getElementById(clearId).style.display = 'none';
            }
            else {
                document.getElementById(imgId).onload = 'imgResizeMax(\'ctl00_PlaceHolderMain_prof_imgUser\', 100);imgResizeTbl(\'ctl00_PlaceHolderMain_prof_imgUser\');';
                document.getElementById(imgId).src = ret;
                document.getElementById('ctl00_PlaceHolderMain_prof_hidImageURL').value = ret;
                setTimeout('imgResizeMax(\'ctl00_PlaceHolderMain_prof_imgUser\', 100);imgResizeTbl(\'ctl00_PlaceHolderMain_prof_imgUser\');', 1);
                setTimeout('imgResizeMax(\'ctl00_PlaceHolderMain_prof_imgUser\', 100);imgResizeTbl(\'ctl00_PlaceHolderMain_prof_imgUser\');', 100);
                document.getElementById(clearId).style.display = '';
            }
        }
    }
    function SelectUserImage() {
        var href = '\u002f_layouts\u002fProfilePic.aspx';
    
    
    
    var features = 'resizable: yes; status: no; scroll: no; help: no; center: yes; dialogWidth: 460px; dialogHeight: 140px; width:460;height:240;menubar:no;directories:no;location:no;';
    commonShowModalDialog(href, features, SelectUserImageCallback, null);
    
    }

  3. 用户单击“确定”按钮后,在 ProfilePic.aspx 页面中。我用一些逻辑上传他的照片然后我用 javascript 关闭窗口

protected void btnOK_Click(Object sender, EventArgs e) {

    try
    {
       // My logic Here

       Debug.WriteLine("Shared Pictures Save Ends: " + DateTime.Now);
       Response.Write ("<script language =javascript>close();</script>");
       Response.End();
    }
    catch (Exception exception)
    {

        LogMessage(exception.Message, EventLogEntryType.Error);
        if (exception.Message.ToLower().Contains("blocked"))
            errorDisplay.Text = "* This type of file has been blocked by the  administrator, please try a different file.";
        else
        {
            errorDisplay.Text = exception.Message;
        }
    }
}

My Question: 我可以关闭窗口,但是,我需要调用回调函数 `SelectUserImageCallback' 不触发。我需要在 OK 按钮部分执行完成后调用此方法。

【问题讨论】:

    标签: c# javascript asp.net sharepoint


    【解决方案1】:

    您是否在回调执行之前关闭窗口?我以前做过。作为一个实验,尝试注释掉关闭窗口的代码。

    您可能需要重新构建代码,以便回调函数在完成任何操作后关闭窗口。

    更新:抱歉,我误解了这个问题。有很多代码,我没有全部阅读。我以为回调在对话框页面中,但看起来它在主页中。我对commonShowModalDialog() 不熟悉,但看起来它可能与SharePoint 有关。您有关于该方法的任何文档吗?我发现this discussion 看起来好像有一种特殊的方法可以从对话框中返回一个值。可能是您的回调没有被调用,因为您没有以正确的方式关闭窗口。 (这完全是我的猜测。)

    祝你好运。

    【讨论】:

    • 如果关闭窗口的注释代码。我的窗口如何自动关闭并调用回调函数?
    • 对不起,@James123,我误会了。我已经用一些猜测更新了我的答案。
    • 有没有其他不使用commonShowModalDialog()。并使用常规的 asp.net 对话框打开另一个 aspx 页面并返回值。
    • jQuery 是一个选项吗,@James123?这个答案讨论了如何使用 jQuery 的对话框功能:stackoverflow.com/q/366696/4794#416900
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2019-12-07
    • 2013-07-29
    • 1970-01-01
    相关资源
    最近更新 更多