将ASP.NET变量分配给Javascript变量 [英] Assign an ASP.NET variable to a Javascript Variable
问题描述
你好,
我的代码中有以下内容
Hi there,
I have the following in my codeBehind
Public Property MenuType as String = "Hello"
在我的JavaScript中,我有:
In my javascript I have:
$(document).ready
(function () {
//If the MenuType is hide then we want to remove the menu from the iPhone,
//but leave it there for the desktop version. The MenuType is defined in the asp.net page
var menuType = "<%=MenuType%>";
alert(menuType);
}
);
加载页面时,出现<%= MenuType%>警报. -也就是说,我得到的是文字表达式,而不是变量的值.
我研究的所有信息都使我相信上述方法应该有效.我在做什么错?
When I load the page I get the alert <%=MenuType%> - that is I get the literal expression rather than the value of the variable.
The information I have researched all leads me to believe that the above should work. What am I doing wrong?
推荐答案
(document).ready (功能 () { // 如果MenuType为hide,那么我们要从iPhone中删除菜单, // ,但将其保留在台式机版本中. MenuType在asp.net页中定义 var menuType = " ; alert(menuType); } );
(document).ready (function () { //If the MenuType is hide then we want to remove the menu from the iPhone, //but leave it there for the desktop version. The MenuType is defined in the asp.net page var menuType = "<%=MenuType%>"; alert(menuType); } );
加载页面时,出现<%= MenuType%>警报. -也就是说,我得到的是文字表达式,而不是变量的值.
我研究的所有信息都使我相信上述方法应该有效.我在做什么错了?
When I load the page I get the alert <%=MenuType%> - that is I get the literal expression rather than the value of the variable.
The information I have researched all leads me to believe that the above should work. What am I doing wrong?
尝试这样..
Hi,
try like this..
//Javascript Function
function AssignValue(Value)
{
var menuType = Value;
alert(menuType);
}
并将值从后面的代码传递给函数
and pass the value to the function from code behind
ScriptManager.RegisterClientScriptBlock(this.Page,this.GetType(),"Myscript","AssignValue('"+ MenuType +"');",true);
这篇关于将ASP.NET变量分配给Javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!