尝试将值(abc)从代码隐藏传递给JavaScript,但页面失败并且无法加载。语法有问题吗?我注意到通常<%...%>突出显示为黄色,但在我的代码中并非如此。
<script src="../Scripts/jqModal.min.js" type="text/javascript"></script>
<script type="text/javascript">
$().ready(function() { });
$("a").click(function() {
if (this.id == "optionalFeatures_Online") {
var abc = "<%=Variable_codebehind %>";
}
});
</script>
On_Load事件背后的代码:
protected override void OnLoad(EventArgs e)
{
Variable_codebehind = "hello world";
}
日志文件错误:
抛出了答案 0 :(得分:0)
首先将值绑定到隐藏控件
然后从隐藏控件中获取值
答案 1 :(得分:0)
<script src="../Scripts/jqModal.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("a").click(function() {
if (this.id == "optionalFeatures_Online") {
var abc = <%=Variable_codebehind %>;
}
});
});
</script>
On_Load事件背后的代码:
protected override void OnLoad(EventArgs e)
{
Variable_codebehind = HttpUtility.JavaScriptStringEncode("hello world", true);
}
答案 2 :(得分:0)
您可以使用Page.RegisterStartupScript并从Code-Behind传递一些变量。将脚本放在.js文件中,然后从代码隐藏的OnLoad方法调用它:
OnLoad CodeBehind:
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", String.Format("MyScript({0});", codeBehindVar));
MyScript.js
function MyScript(myVar)
{
var self = this;
$("a").click(function() {
if (this.id == "optionalFeatures_Online") {
var abc = self.myVar;
}
}