簡體   English   中英

從后面的代碼調用時,Javascript函數不起作用

[英]Javascript Function is not working when calling from code behind

我在.aspx頁面中有此javascript函數。

<script type="text/javascript">

    function somefun(value) {
        document.getElementById("myFlash").SetVariable("player:jsUrl", value);
        document.getElementById("myFlash").SetVariable("player:jsPlay", "");
    }

當我在.aspx頁面的主體內部這樣調用它時,它可以完美工作

<button onclick="somefun('Audio/filename.mp3')">English</button>

但是,如果我在click事件背后代碼中調用它,則會出現錯誤。

protected void theClickEvent(object sender, EventArgs e)
    {
        string filePath1 = "Audio/filename.mp3";
        ScriptManager.RegisterClientScriptBlock(this, typeof(string), "Registering", String.Format("somefun('{0}');", filePath1), true);
    }

當我單擊按鈕時,出現此錯誤: “無法獲取未定義或空引用的屬性'SetVariable'”

我通過“ myflash”調用對象

<object id="myFlash" type="application/x-shockwave-flash" data="player_mp3_maxi.swf" width="1093" height="52" >

會是什么問題?

編輯:

<head runat="server">

<script type="text/javascript">

    function somefun(value) {

        alert(document.getElementById("myFlash"));

        document.getElementById("myFlash").SetVariable("player:jsUrl", value);
        document.getElementById("myFlash").SetVariable("player:jsPlay", "");

    }

<form id="form1" runat="server">

        <asp:LinkButton ID="LinkButton1" Text = "Download"   runat="server" OnClick = "theClickEvent"></asp:LinkButton>         
</form>


<object id="myFlash" type="application/x-shockwave-flash" data="player_mp3_maxi.swf" width="1093" height="52" style="margin-left:0%">

</object>

您正在將JavaScript代碼注冊到Button的click事件中,該事件當時不執行。

在向腳本管理器注冊腳本之前,將執行客戶端代碼onclick="somefun('Audio/filename.mp3')"

Page_Load使用您的代碼塊,而不是按鈕單擊事件。

更新

似乎Flash對象需要花費一些時間才能加載,或者在加載之前單擊。 為避免這種情況,您可以執行以下操作。

  1. 禁用鏈接按鈕,直到DOM(或JavaScript中的Document)准備就緒
  2. 准備好DOM /文檔后,使用JavaScript啟用按鈕。
  3. 另外,在訪問元素的任何屬性之前進行Null檢查也是一個好習慣

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM