[英]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對象需要花費一些時間才能加載,或者在加載之前單擊。 為避免這種情況,您可以執行以下操作。
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.