从代码隐藏中获取C#变量到aspx页面上的javascript变量 [英] Get C# variable from codebehind into javascript variable on aspx page

查看:245
本文介绍了从代码隐藏中获取C#变量到aspx页面上的javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c#和asp.net的初学者。我的代码后面有一个公共十进制变量。如何使用这个变量并在我的aspx页面上使用javascript的if语句中使用它?...任何帮助将不胜感激。

Hi, i am beginner at c# and asp.net. I have a public decimal variable on in my code behind. How do iget this variable and use it in a if statement with javascript on my aspx page?...Any help will be much appreciated.

推荐答案

那么有一个我们以前在ASP时使用的快捷方式,这仍然适用于ASP.NET

在页面中的codebehind声明一个公共属性说public int MyInteger

在aspx中放这个



Well there is one shortcut we used to use during the ASP days, this still works in ASP.NET
In the page codebehind declare a public property say public int MyInteger
In the aspx put this

<script>
    function GetMyValue()
    {
     var someVar = <%=this.MyInteger%>;
     //Do something
    }
</script>





为了做得好,你可以使用RegisterClientScriptBlock [ ^ ]





To do it nicely, you can use RegisterClientScriptBlock [^]

string script = string.Format("var myVariable = '{0}';", vaiable.ToString());
if (!ClientScript.IsClientScriptBlockRegistered("myScript"))
{
    ClientScript.RegisterClientScriptBlock(typeof(_Default), "myScript", script, true);
}


public decimal Price;





然后你就可以从Javascript访问它了





then you can access that from Javascript as

var price = <%=this.Price%>;


public int i;

i = 10;

//做一些代码....




查看页面上的
....

用jquery函数或任何写入事件

var price =<%= this.i%>;
public int i;
i=10;
//do some code....


on view page....
write with in jquery function or any event
var price = <%=this.i%>;


这篇关于从代码隐藏中获取C#变量到aspx页面上的javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆