2011-05-18 162 views
1

我爲我的web應用程序的背後我的代碼,我有相關的情況下,本方法點擊傳遞變量給JavaScript

protected void Button1_Click(object sender, EventArgs e) 
{ 

     string pagFinal = "this is a test"; 

     ClientScript.RegisterClientScriptBlock(this.GetType(), "variable", "<script language=javascript> var direction = <%=pagFinal%> </script>"); 

} 

,但是當我看到產生不出現的的JavaScript的HTML爲什麼我做錯了。由於

+0

你嵌入式服務器端代碼在您的JS代碼片段。然而,這將被直接輸出 – 2011-05-18 14:52:33

回答

6

您可以直接在你的代碼替換變量值 -

ClientScript.RegisterClientScriptBlock(this.GetType(), "variable", "<script language=javascript> var direction = '" + pagFinal + "'; </script>");