我正在学习ASP.net, 我在 C# 后面的代码中有一个变量:
public int hasCar= 1;
在 aspx 文件中,我想在 javascript 函数中访问此变量:
function PrintCar( ) {
var ind = <%=this.hasCar%>
alert(ind);
}
但我收到错误:
does not contain a definition for 'hasCar' and no extension method 'hasCar' accepting a first argument of type 'ASP.vids_aspx' could be found (are you missing a using directive or an assembly reference?)
出了什么问题?
谢谢
最佳答案
这对我有用:
ASPX 页面:
<body>
<script type="text/javascript">
function PrintCar( ) {
var ind = <%=this.HasCar%>
alert(ind);
}
</script>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="PrintCar();"/>
</form>
</body>
代码隐藏
public partial class WebForm1 : System.Web.UI.Page
{
public int HasCar { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
HasCar = 1;
}
}
关于c# - 从 JavaScript 中访问 C# 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21425953/