将变量从cs传递给javascript

时间:2011-05-18 14:45:15

标签: c# asp.net

我是我的Web应用程序背后的代码,我将此方法与事件相关联单击

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>");

}

但是当我看到生成的html没有出现时,javascript为什么我做错了。感谢

1 个答案:

答案 0 :(得分:6)

您可以直接替换代码中的变量值 -

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