简体   繁体   中英

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. I want those value in my C# code using loop. 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

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[] hiddenValues = hdnCommaSeparatedString.Split(',');

Eg "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

Case1:

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:

Hidden field names are unique, you can extract the values like following:

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. So, the html code should be something like:

<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.

Then in the C# code behind you should handle the posted form like this:

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. So Keys == Names.

You have the key, so you can get the value, by using the Get method of the Request.Form property. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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