TOP

[C#] 在C# codebehind執行JavaScript

問題:因為程式取得在Codebehind內取得JavaScript,而且還要在Codebehind內執行此段JavaScript的function


  • Page.ClientScript.RegisterClientScriptBlock
  • Page.ClientScript.RegisterStartupScript
  • Page.ClientScript.RegisterClientScriptInclude
string Script=@”function ShowName() 
var txt=document.getElementById(‘txtName’);
 alert(‘Your name is: ‘+txt.value);
 }”;

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “ShowName”, Script, true);


參考資料 :
http://blog.mdsohelrana.com/2008/06/14/how-to-add-javascript-at-runtime-using-c-in-aspnet-20/




另外一種方式是Codebehind呼叫前端的JavaScript


A.aspx
<script type="text/javascript">
   
    function Popup(msg)
    {
        alert('訊息視窗 = ' + msg);
    }

</script>

A.aspx.cs
Page.RegisterStartupScript("Show", "<script language=JavaScript>Popup('"+字串+"');</script>");



參考資料 :

http://www.dotblogs.com.tw/aquarius6913/archive/2010/11/09/19310.aspx


0 意見:

張貼留言