簡體   English   中英

如何將變量值后面的代碼綁定到javascript?

[英]How to bind code behind variable value to javascript?

我有一個javascript標頭<script type="text/javascript" src="http://example.com?Key=">這個Key在aspx.cs中,如string Key="123456"; 如何將此Key值綁定到javascript頭?

您可以訪問aspx文件中的變量后面的代碼:

<script type="text/javascript" src="http://example.com?Key=<%= Key %>">

代碼背后:

public Key { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Key = "123456";
    }
}

標記:

 <script type="text/javascript" src="http://example.com?Key=<%= Key %>">
 </script>

試試這種方式:

1 - 通過添加id和runat =“server”屬性使您的腳本可以通過代碼隱藏引用:

<script id="myScript" runat="server" type="text/javascript">

2-in code-behind,在page_load上,動態添加src屬性:

this.myScript.Attributes.Add("src","http://example.com?Key=" + Key);

你完成了!

暫無
暫無

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

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