从后面的代码设置javascript变量的值(C#) [英] Setting value for javascript variable from code behind (C#)

查看:45
本文介绍了从后面的代码设置javascript变量的值(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解客户端和服务器端脚本之间的区别。我的 MasterPage 中有一个javascript函数和变量:

I understand the difference between Client-side and Server-side scripting. I have a javascript function and variable in my MasterPage:

<script language="JavaScript" type="text/javascript">
    var needToConfirm = false;
          window.onbeforeunload = confirmExit;
          function confirmExit()
          {
            if (needToConfirm)
            {
              needToConfirm = false;
              return "Currently in edit mode. If you leave the page now then you will lose unsaved changes."
            }
          }
</script>

鉴于在我的ASP.NET(客户端)上我可以更改我的值 needToConfirm 变量 true onClientClick 但默认情况下为false 。这是一个例子。

Given the fact that on my ASP.NET (Client-side) I can change the value of my needToConfirm variable to true onClientClick but by default it is false. Here's an example.

 <asp:Button ID="btnEdit" runat="server" Text="  Edit  " onclick="btnEdit_Click" OnClientClick="needToConfirm = true;" />

现在问题是在C#(服务器端)上我必须设置 needToConfirm if-statement 下为true,但不一定在 Page_Load

Now the question here is when on the C# (Server-side) I have to set the needToConfirm to true under an if-statement but not necessarily on Page_Load:

private void SetDefault()

    if (Session[def.ID_CUST] != null)
    {
          //I want to change the variable value here
    }
}

谢谢。

更新

我' m使用.NET 2.0 Classic和WebForms

I'm using .NET 2.0 Classic and WebForms

推荐答案

代码背后:

ScriptManager.RegisterStartupScript(this, this.GetType(), "", "urFunction('urValHere');", true);

function urFunction(urParam) {
        //do what u want here
        //use urParam
    }

这篇关于从后面的代码设置javascript变量的值(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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