简体   繁体   English

获取C#中由JavaScript创建的隐藏字段

[英]get hidden field in C#, created by javascript

I have a javascript that can create hidden filed in my form dynamically, also generation of the hidden fields are variable it can be 200 and 1000 also. 我有一个可以在我的表单中动态创建隐藏文件的JavaScript,隐藏字段的生成也是可变的,它也可以是200和1000。 I want those value in my C# code using loop. 我想在我的C#代码中使用循环这些值。 I have tried below code but it give me only the first value static, but I need values in loop so that I can access all the values and store in my SQL database 我已经尝试过下面的代码,但它只给我第一个静态值,但是我需要循环使用值,以便可以访问所有值并将其存储在SQL数据库中

string valueOfTheHiddenField = this.Request.Form.Get("0");

Example this type of code needed 此类所需代码的示例

for each
{
    string HiddenFieldvalue = this.Request.Form.Get("0");
    insert into sql = value Of The HiddenField
}

Below is the sample code i am using 以下是我正在使用的示例代码

var form = document.forms['form1'];
for (var i = 0; i < 150; i++) {
    var el = document.createElement("input");
    el.type = "hidden";
    el.name = "myHiddenField";
    el.value = trial2;
    el.id = i;
    form.appendChild(el);
}

These hidden fields are generated 这些隐藏字段生成

<input id="0" type="hidden" name="myHiddenField" value=" Root Canal Treatment ">
<input id="1" type="hidden" name="myHiddenField" value=" Cosmetic Dentistry ">
<input id="2" type="hidden" name="myHiddenField" value=" Fillings ">
<input id="3" type="hidden" name="myHiddenField" value=" Apicectomy ">
<input id="4" type="hidden" name="myHiddenField" value=" Aesthetic Crown And Bridges ">
<input id="5" type="hidden" name="myHiddenField" value=" Bleaching "> 

As in the case of your problem, You can access the hidden field(even form field) values by their name as shown below: 就像遇到问题一样,您可以按其名称访问隐藏字段(甚至表单字段)值,如下所示:

string hdnCommaSeparatedString = Request.Form["myHiddenField"];

This will give you a string of comma separated values of all the hidden fields, which you can split using string.split(...) overload to get the values. 这将为您提供所有隐藏字段的逗号分隔值的字符串,您可以使用string.split(...)重载对其进行拆分以获取这些值。

string[] hiddenValues = hdnCommaSeparatedString.Split(',');

Eg "Root Canal Treatment ,Cosmetic Dentistry ...." //This you can split using server side logic 例如, "Root Canal Treatment ,Cosmetic Dentistry ...." //This you can split using server side logic

Note This will break if the value in hidden field contains a comma and when you try to string.split in code behind with a comma(,) you will see unexpected string breaks. 注意如果隐藏字段中的值包含逗号,则这将中断,并且当您尝试使用逗号(,)在字符串中拆分字符串时,您将看到意外的字符串中断。

Eg 例如

 <input id="0" type="hidden" name="myHiddenField" value=" Root Canal, Treatment "><!--Notice a comma after Root Canal in the value & this will yield unexpected values if you string split on code behind.-->
 <input id="1" type="hidden" name="myHiddenField" value=" Cosmetic Dentistry ">

EDIT 编辑

Based on comments following are other cases to get form field values through Request.Form 根据注释,以下是通过Request.Form获取表单字段值的其他情况

Case1: 情况1:

Hidden field names are NOT unique as in your case: 隐藏的字段名称不是唯一的,例如您的情况:

string[] hiddenFieldValueList = Request.Form.GetValues("myHiddenField");
//This allows parsing of hidden/form field values having comma in them 

Case2: 情况2:

Hidden field names are unique, you can extract the values like following: 隐藏的字段名称是唯一的,您可以提取以下值:

HTML 的HTML

<input id="0" type="hidden" name="myHiddenField0" value=" Root Canal, Treatment ">
<input id="1" type="hidden" name="myHiddenField1" value=" Cosmetic Dentistry ">
<input id="2" type="hidden" name="myHiddenField2" value=" Fillings, ">
<input id="3" type="hidden" name="myHiddenField3" value=" Apicectomy ">
<input id="4" type="hidden" name="myHiddenField4" value=" Aesthetic Crown And Bridges ">
<input id="5" type="hidden" name="myHiddenField5" value=" Bleaching "> 

Code Behind 背后的代码

var hiddenFieldValueList = Request.Form.AllKeys.Where(key => key.StartsWith("myHidden")).Select(it => Request.Form[it]).ToList();

Hope this help you.. 希望对您有帮助。

For clarity you should create a unique name for every hidden field because if you don't, back in the code, the request will receive only one key, the 'myHiddenField', which will hold all the values. 为了清楚起见,您应该为每个隐藏字段创建一个唯一的名称,因为如果您不这样做,则返回到代码中,该请求将仅接收一个键“ myHiddenField”,该键将保存所有值。 So, the html code should be something like: 因此,html代码应类似于:

<input id="0" type="hidden" name="myHiddenField0" value=" Root Canal Treatment ">
<input id="1" type="hidden" name="myHiddenField1" value=" Cosmetic Dentistry ">
<input id="2" type="hidden" name="myHiddenField2" value=" Fillings ">
<input id="3" type="hidden" name="myHiddenField3" value=" Apicectomy ">
<input id="4" type="hidden" name="myHiddenField4" value=" Aesthetic Crown And Bridges ">
<input id="5" type="hidden" name="myHiddenField5" value=" Bleaching "> 

Just append the index at the end of every name in the JS code. 只需在JS代码中每个名称的末尾附加索引。

Then in the C# code behind you should handle the posted form like this: 然后,在后面的C#代码中,您应该像这样处理已发布的表单:

var keys = Request.Form.AllKeys;
foreach (var key in keys)
{
    string value = Request.Form.Get(key);
}

By accessing the AllKeys property of the posted Form you get all the different keys posted, which are the names of the respective input fields. 通过访问已发布表单的AllKeys属性,可以获取所有已发布的不同键,它们是相应输入字段的名称。 So Keys == Names. 因此,键==名称。

You have the key, so you can get the value, by using the Get method of the Request.Form property. 您具有密钥,因此可以通过使用Request.Form属性的Get方法获取值。 This accepts either an index or the key (name) of the input field. 这接受输入字段的索引或键(名称)。

Then as soon as you've got the value you can proceed with your scenario. 然后,一旦您获得了价值,就可以继续进行方案。

Hope this helps. 希望这可以帮助。

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

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