如何放置应用程序';s值作为javascript中的变量?

如何放置应用程序';s值作为javascript中的变量?,javascript,asp.net,Javascript,Asp.net,我有一个计数器,每次加载页面时都会自动更新。我想在javascript中将该计数器的值用作变量,以便使用它。我该怎么做 if (Application["counter"] == null) { Application["counter"] = 0; } if (Application["counter"] != null) {

我有一个计数器,每次加载页面时都会自动更新。我想在javascript中将该计数器的值用作变量,以便使用它。我该怎么做


    if (Application["counter"] == null)
        {
            Application["counter"] = 0;
        }
        if (Application["counter"] != null)
        {
            if (Request.Form["sub1"]==null)
            {
                Response.Write("<script>alert('You must Enter Number')</script>");
            }
            if (Request.Form["sub1"] != null)
            {
                Application["counter"] =(int)Application["counter"]+ 1;
                Response.Write(Application["counter"]);
            }
        }


if(应用程序[“计数器”]==null)
{
应用程序[“计数器”]=0;
}
if(应用程序[“计数器”]!=null)
{
if(Request.Form[“sub1”]==null)
{
响应。写入(“警报(‘您必须输入编号’)”);
}
if(Request.Form[“sub1”]!=null)
{
应用程序[“计数器”]=(int)应用程序[“计数器”]+1;
写(应用程序[“计数器]);
}
}
这是cs页面中计数器的代码。 每当我编写Javascript并试图将其保存为变量时,它都会给我一个错误:错误16只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句

var y=<%Application["counter"];>%;
var y=%;

对于服务器端代码,您可以如下所示使用它。只是一个建议。至于前端,你有打字错误

// Server side code - Correct usage

if (Application["counter"] == null) {
  Application["counter"] = 0;
}

// there's no need to check Application["counter"] after the code above

if (Request.Form["sub1"] == null)
{
  Response.Write("<script>alert('You must Enter Number')</script>");
}
// also, you can use else in here instead of another if, because there isn't any other condition.
else { 
  Application["counter"] = (int)Application["counter"] + 1;
  Response.Write(Application["counter"]);
}

// JS Code

var y = <%= Application["counter"] %>;
//服务器端代码-正确用法
if(应用程序[“计数器”]==null){
应用程序[“计数器”]=0;
}
//在上面的代码之后,不需要检查应用程序[“计数器”]
if(Request.Form[“sub1”]==null)
{
响应。写入(“警报(‘您必须输入编号’)”);
}
//另外,您可以在这里使用else而不是另一个if,因为没有任何其他条件。
否则{
应用程序[“计数器”]=(int)应用程序[“计数器”]+1;
写(应用程序[“计数器]);
}
//JS代码
变量y=;
var y=同一行中有两个打字错误