簡體   English   中英

C#asp.net調用javascript

[英]C# asp.net calling javascript

我在asp里面有一個div:內容:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="slidebar" style="display:none;"  >There are some pending approvals.Please    approve    by the 25th of this month

<a href="#" id="slink" style="margin-left:10px;" onclick="fade('slidebar');">Click here to   close <sup style="font:caption;color:#373636;">X</sup></a>
</div>

<script language="javascript" type="text/javascript">
 function showbanner()
  {
   document.getElementById("slidebar").style.visibility="visible";
  }
</script>
<\asp:content>

和背后的代碼:

 ClientScript.RegisterClientScriptBlock(Page.GetType(), "displaybanner", "return  showbanner();", true);

我無法從后面的代碼中調用函數showbanner,甚至當我直接使用registerclientscript調用showbanner中的語句時......它也不會被調用。

請幫忙

屬性visibilitydisplay不一樣,將js函數更改為:

function showbanner()
{
    document.getElementById("slidebar").style.display="block";
}

也將你的代碼改為

ClientScript.RegisterStartupScript(Page.GetType(), "displaybanner", "showbanner();", true);

所以腳本在頁面加載后執行,否則它將找不到元素。

我不確定為什么你的注冊腳本不能正常工作,但你是否嘗試將javascript調用直接放在腳本塊內的頁面上? 如果可行,則將腳本塊包裝在頁面底部的pannel中,其中visible =“false”。 在您確定要使用它的代碼后面,將可見性設置為true。

如果您正在使用Jquery,我還會將腳本塊的內容包裝在:

$(document).ready(function() {
    //javascript call here
});

只是為了確保在頁面實際處理它之前不會調用它。

這一切都假定您的函數調用是好的,並且問題在於寄存器客戶端腳本。

好吧,我的團隊成員Mahi想出了一個解決方案...而不是CleientScript.reg .....

我們使用Page.RegisterStartupScript,它工作正常:))

暫無
暫無

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

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