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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16474640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:05:36  来源:igfitidea点击:

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

c#javascriptasp.net

提问by Christian Mark

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

我了解客户端和服务器端脚本之间的区别。我有一个 javascript 函数和变量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>

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

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

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

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

现在这里的问题是在 C#(服务器端)上我必须needToConfirm在 an 下设置为 trueif-statement但不一定在Page_Load

private void SetDefault()

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

Thanks.

谢谢。

UPDATE

更新

I'm using .NET 2.0 Classic and WebForms

我正在使用 .NET 2.0 Classic 和 WebForms

采纳答案by Vond Ritz

in code behind:

在后面的代码中:

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

on client-side:

在客户端:

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

回答by Semih Yagcioglu

You can use a hidden input and then set this input to trueor falsefrom the server side.

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

On the client side:

在客户端:

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

Then on the server side:

然后在服务器端:

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

Then on the client side:

然后在客户端:

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

回答by Steven V

If I understand this correctly, you could register a client script like in the example at http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx.

如果我理解正确,您可以像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);
}

That would write a script to the page setting the value of needToConfirmin Javascript.

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

回答by LouD

Based on your update saying it's .NET 2.0, this is how you can set a javascript variable:

根据您的更新说它是 .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

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

回答by Todd Vance

Just for reference. Here is the 4.5 way to do something like this:

仅供参考。这是执行此类操作的 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

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

回答by hunteroooox

Maby this helps?

玛比这有帮助吗?

<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);" />