简体   繁体   English

获取c#中隐藏输入字段的值

[英]get value of hidden input field in c#

I have a charts program that works in js, but I need to update excels with the data using c#. 我有一个在js中工作的图表程序,但我需要使用c#更新excel与数据。 So, what I've done so far is to create hidden input fields whose values are updated with the js variable: 所以,到目前为止我所做的是创建隐藏的输入字段,其值使用js变量更新:

<input type="hidden" name="baselineInput"  id="baselineVariable" />
<input type="hidden" name="goalInput" runat="server" id="goalVariable" />
<input type="hidden" name="currentInput" runat="server" id="currentVariable" />

//do some things here
var MTDGauge = new Array(5,5,5);

document.getElementById("baselineVariable").value = MTDGauge[0];
document.getElementById("goalVariable").value = MTDGauge[1];
document.getElementById("currentVariable").value = MTDGauge[2];

The problem I'm having is reading these values back to the c# variable. 我遇到的问题是将这些值读回c#变量。 Here's what I've tried (placed after the code above): 这是我尝试过的(放在上面的代码之后):

    List<string> OverallData = new List<string>();

    OverallData.Add(Request["baselineInput"]);
    OverallData.Add(Request["goalInput"]);
    OverallData.Add(Request["currentInput"]);

This is just adding null values (assuming so because the c# is loading before the javascript and the initialized values for the text fields are null?). 这只是添加空值(假设是这样,因为c#在javascript之前加载,文本字段的初始化值为null?)。 I thought of creating ac# function that could be called using the onchange option in the input field, but that didn't get me anywhere. 我想创建可以使用输入字段中的onchange选项调用的ac#函数,但这并没有让我到处都是。 Is there something I'm missing? 有什么我想念的吗? Do I have to do a post? 我必须做一个帖子吗?

Thanks 谢谢

Yes, to get your data back to the server (where your C# is running) you need to send it back. 是的,要将数据恢复到服务器(运行C#的位置),您需要将其发回。

POST is one way, or GET to add the values to the query string. POST是一种方式,或GET将值添加到查询字符串。

Javascript is only running in the browser, until you send something back to the server, your ASP.NET app will not have any information. Javascript只在浏览器中运行,直到您将某些内容发送回服务器,您的ASP.NET应用程序将不会有任何信息。

Of course, if you want to do it asynchronously you could use jQuery.ajax to send the data to a server URL without refreshing the whole page. 当然,如果您想异步执行此操作,可以使用jQuery.ajax将数据发送到服务器URL而无需刷新整个页面。

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

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