[英]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.