简体   繁体   English

将javascript变量传递到服务器端C#逻辑

[英]Passing javascript variables to server-side C# logic

Is there a way to assign/pass/copy a javascript variable to a server side variable in C#? 有没有办法在C#中将javascript变量分配/传递/复制到服务器端变量? For the sake of argument, let's say that I was able to parse some JSON for variables that I want to store and assign them on the client (ie. var = FirstName and var = 25 and var = someDateTime, etc) . 为了便于讨论,假设我能够解析一些JSON,以获取要存储的变量并在客户端上分配它们(即var = FirstName和var = 25以及var = someDateTime等)。

Javascript variables exist on the client so in order to get those values into the server you'll need to execute a request from the client. 客户端上存在Javascript变量,因此为了将这些值输入服务器,您需要执行来自客户端的请求。 You probably want an approach called AJAX. 您可能需要一种称为AJAX的方法。 AJAX involves Javascript making requests to the server in the background of your page. AJAX涉及Javascript在页面后台向服务器发出请求。 You'll set up a C# web page that expects these background requests. 您将建立一个C#网页,期望这些后台请求。 If you use a GET request then then place the variables in the query string of your AJAX request to your new C# page. 如果您使用GET请求,则将变量放在AJAX请求的查询字符串中,以放入新的C#页面。 If you want to use a POST request then you'll set parameters in the data that you post to your page. 如果要使用POST请求,则将在发布到页面的数据中设置参数。

Libraries like jQuery make this kind of thing pretty simple. jQuery之类的库使这种事情变得非常简单。

There's no direct way to access variables in client-side code from your server-side code. 没有直接方法可以从服务器端代码访问客户端代码中的变量。

An easy way, without writing handlers, ajax posts, etc., to accomplish this is to simply store the java script variable in a hidden text box and retrieve it on your post. 一种无需编写处理程序,ajax帖子等的简单方法即可完成此操作,只需将java脚本变量存储在一个隐藏的文本框中,然后在您的帖子中检索它即可。 You can also write back to the hidden field and feed your script with the value, eg 您还可以写回隐藏字段,并使用以下值来输入脚本:

Markup 标记

<asp:HiddenField runat="server" Id="JavascriptValue" value="0">

Script 脚本

<script>
   var myValue = <%=JavascriptValue.Value%>
</script>

Server-Side 服务器端

protected void Page_Load(object sender, EventArgs e)
{
string val = JavascriptValue.Value;
}

what I did is save the javaScript variable in a cookie and then read it from C#. 我所做的是将javaScript变量保存在cookie中,然后从C#中读取它。

JavaScript Code: JavaScript代码:

<script>
$(document).ready(function () {
   createCookie("height", $(window).height(), "10");
 });

 function createCookie(name, value, days) {
   var expires;
   if (days) {
     var date = new Date();
     date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
     expires = "; expires=" + date.toGMTString();
   } else {
     expires = "";
   }
   document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
  }
</script>  

C# Code: C#代码:

height of browser:@Request.Cookies["height"].Value;   

Write the value to a control (eg HiddenField ) via JS and read that on the postback. 通过JS将值写入控件(例如HiddenField )并在回发中读取。

You can register hidden fields from code-behind on the Page_Load 您可以在Page_Load代码隐藏中注册隐藏字段

if (this.Request.Form["myHiddenField"] == null) { 
    ClientScript.RegisterHiddenField("myHiddenField", ""); }

populate it with a script 用脚本填充它

ClientScript.RegisterOnSubmitStatement(this.GetType(),
     MethodBase.GetCurrentMethod().DeclaringType.Name + "_myHiddenField",
     "var res=document.getElementById('myHiddenField');if(res!=null){res.value='some value';}");

and read it on postbacks (also Page_Load ) 并在回发中阅读(也为Page_Load

var myValue = (!IsPostBack)
    ? null
    : this.Request.Form["myHiddenField"];

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

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