繁体   English   中英

从后面的代码设置javascript变量的值(C#)

[英]Setting value for javascript variable from code behind (C#)

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

<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。 这是一个例子。

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

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

private void SetDefault()

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

谢谢。

UPDATE

我正在使用.NET 2.0 Classic和WebForms

代码背后:

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

在客户端:

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

您可以使用隐藏输入,然后从服务器端将此输入设置为truefalse

在客户端:

<input type="hidden" id="hdnConfirm" runat="server" value="false"/>

然后在服务器端:

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

然后在客户端:

var needToConfirm = $('#hdnConfirm').val();

如果我理解正确,您可以注册客户端脚本,如http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx中的示例。

ClientScriptManager cs = Page.ClientScript;
if (!cs.IsStartupScriptRegistered(this.GetType(), "EditMode")) {
    cs.RegisterStartupScript(this.GetType(), "EditMode", "needToConfirm = true;", true);
}

这会将一个脚本写入页面,在Javascript中设置needToConfirm的值。

根据您的更新说它是.NET 2.0,这是您可以设置javascript变量的方法:

Page.RegisterStartupScript("SetVar", "var needToConfirm = true;");

http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript(v=vs.80).aspx

仅供参考。 这是4.5方式做这样的事情:

 // Define the name and type of the client scripts on the page.
 const String csname1 = "MyScriptName";
 Type cstype = this.GetType();

 // Get a ClientScriptManager reference from the Page class.
 ClientScriptManager cs = Page.ClientScript;

 // Check to see if the startup script is already registered.
 if (!cs.IsStartupScriptRegistered(cstype, csname1))
 {
      StringBuilder cstext1 = new StringBuilder();
      cstext1.Append("<script> var myVariable = true; </");
      cstext1.Append("script>");

      cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
  }

http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript(v=vs.110).aspx

Maby有帮助吗?

<script type="text/javascript">
    function test(confirm){
        var needToConfirm = confirm;
        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:Button ID="btnEdit" runat="server" Text="Edit" onclick="btnEdit_Click" OnClientClick="javascript:test(true);" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM