js在页面内部工作但不在后面的代码中

时间:2010-04-17 16:23:05

标签: asp.net javascript

我把这个功能放在MasterPage中,它显示了一个mp3播放器:

<script type="text/javascript">
    $(document).ready(function() {
        var stageW = 500;
        var stageH = 216;
        var cacheBuster = Date.parse(new Date());

        var flashvars = {};
        var params = {};

        params.bgcolor = '#F6F6F6';
        params.allowfullscreen = 'true';


        flashvars.stageW = stageW;
        flashvars.stageH = stageH;

        flashvars.pathToFiles = '';

        flashvars.settingsPath = '../mp3player/mp3player_settings.xml';
        flashvars.xmlPath = '<%# getRestXmlPlayerUrl() %>';

        flashvars.keepSelected = 't';
        flashvars.selectedWindow = '4';
        flashvars.slideshow = 't';

        flashvars.imageWidth = '130';
        flashvars.imageHeight = '130';

        swfobject.embedSWF('swf/preview.swf?t=' + cacheBuster, 'myContent', stageW, stageH, '9.0.124', 'swf/expressInstall.swf', flashvars, params);
    }); 
    </script>

一切都很棒。 但是,因为我在页面上有一些带有更新面板的ajax,当ajax请求发生时,flash没有呈现,所以我需要注册这个函数,我尝试过这样的事情:

protected void Page_PreRender(object sender, EventArgs e)
    {
    Type cstype = this.GetType();
    String csnameForPlayer = "applyStyleToMp3Player";
        if (!Page.ClientScript.IsClientScriptBlockRegistered(cstype, csnameForPlayer))
        {
            StringBuilder cstextForPlayer = new StringBuilder();
            cstextForPlayer.Append(" $(document).ready(function() { "
    + " var stageW = 500;"
    + " var stageH = 216;"
    + " var cacheBuster = Date.parse(new Date());"

    + " var flashvars = {};"
    + " var params = {};"

    + " params.bgcolor = '#F6F6F6';"
    + " params.allowfullscreen = 'true';"


    + " flashvars.stageW = stageW;"
    + " flashvars.stageH = stageH;"

    + " flashvars.pathToFiles = '';"

    + " flashvars.settingsPath = '../mp3player/mp3player_settings.xml';"
    + " flashvars.xmlPath = '<%# getRestXmlPlayerUrl() %>';"

    + " flashvars.keepSelected = 't';"
    + " flashvars.selectedWindow = '4';"
    + " flashvars.slideshow = 't';"

    + " flashvars.imageWidth = '130';"
    + " flashvars.imageHeight = '130';"

   + " swfobject.embedSWF('swf/preview.swf?t=' + cacheBuster, 'myContent', stageW, stageH, '9.0.124', 'swf/expressInstall.swf', flashvars, params);"
   + "});   ");
        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), csnameForPlayer, cstextForPlayer.ToString(), true);
    }
}

嗯,这不起作用。 Flash播放器不再出现了,因此,我认为cstextForPlayer出了问题。

我花了一个多小时来搞清楚,但我失败了。

有没有人看到这个问题?

提前致谢。

2 个答案:

答案 0 :(得分:3)

我坦率地不了解代码隐藏,但这一行:

+ " flashvars.xmlPath = '<%# getRestXmlPlayerUrl() %>';"

......看起来可能是一个问题。不应该是:

+ " flashvars.xmlPath = '" + getRestXmlPlayerUrl() + "';"

......还是什么?

答案 1 :(得分:3)

<%# getRestXmlPlayerUrl() %>是造成问题的部分。在RegisterStartupScript中使用时,它不会返回任何值,但会被硬编码。现在我没有经历混合C#和javascript的所有痛苦,而是建议你将这个脚本外化到一个函数中,最好使用正确的工具来做正确的事情:

function embedPlayer(xmlPath) {
    var stageW = 500;
    var stageH = 216;
    var cacheBuster = Date.parse(new Date());

    var flashvars = {};
    var params = {};

    params.bgcolor = '#F6F6F6';
    params.allowfullscreen = 'true';


    flashvars.stageW = stageW;
    flashvars.stageH = stageH;

    flashvars.pathToFiles = '';

    flashvars.settingsPath = '../mp3player/mp3player_settings.xml';
    flashvars.xmlPath = xmlPath;

    flashvars.keepSelected = 't';
    flashvars.selectedWindow = '4';
    flashvars.slideshow = 't';

    flashvars.imageWidth = '130';
    flashvars.imageHeight = '130';

    swfobject.embedSWF('swf/preview.swf?t=' + cacheBuster, 'myContent', stageW, stageH, '9.0.124', 'swf/expressInstall.swf', flashvars, params);
}

这会简化您的服务器端代码,只需调用前一个函数并将xml路径作为参数传递:

string csnameForPlayer = "applyStyleToMp3Player";
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), csnameForPlayer))
{
    var script = string.Format("embedPlayer('{0}');", this.getRestXmlPlayerUrl());
    ScriptManager.RegisterStartupScript(
        this.Page, 
        this.Page.GetType(), 
        csnameForPlayer, 
        script, 
        true
    );
}