Click here to Skip to main content
15,881,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am getting array values in asp.net hidden field in javascript but the last value is giving at the code behind can anybody tell me the solution for this issue

What I have tried:

let optionSelected = Array.from(document.getElementsByName('options' + i)).find(r => r.checked).value;
var hdnRadioButtonSelectedOption = document.getElementById("hdnRadioButtonSelectedValue");
hdnRadioButtonSelectedOption.value = optionSelected;
alert(hdnRadioButtonSelectedOption.value);
Posted
Updated 1-Jan-19 20:36pm
Comments
Ehsan Sajjad 2-Jan-19 0:23am    
how does it looks in rendered html ?
Member 8583441 2-Jan-19 0:42am    
string strSelected = hdnRadioButtonSelectedValue.Value;
Nitin S 2-Jan-19 1:34am    
can you create a fiddle of what you have done?
Member 8583441 2-Jan-19 1:59am    
https://jsfiddle.net/pkalyan/jpr9gq04/1/
Prifti Constantine 2-Jan-19 2:25am    
I would recommend Serializing the array Data , Store it inside the hiddenControl.Value
and then Deserialize it when retreiving it in the code behind.

1 solution

Quite generalized according to your given code but i hope this will help you out!

<pre lang="c#">string[] newArray = new string[2];
            string SerializedData = Newtonsoft.Json.JsonConvert.SerializeObject(newArray);
            //The serialized Array will have a Json format and it will be stored inside the hidden COntrol Value
            HiddenCOntrol.value = SerializedData;
            //you can get the Serialized Data from the hidden control Accordingly by desirializing it to your specified object:
            string[] DesirializedData = (string[])Newtonsoft.Json.JsonConvert.DeserializeObject(HiddenControl.Value);
 
Share this answer
 
Comments
Member 8583441 2-Jan-19 3:15am    
in this solution javascript is not presented
Prifti Constantine 2-Jan-19 3:50am    
If the specified control exists then why would you need javascript? Could you please explain your way of thinking? you want to send it through ajax maybe? Create a __doPostback() maybe?
Member 8583441 2-Jan-19 4:04am    
why i am giving in javascript because i am doing online exam in which exam page is designed in code behind. for that purpose i required the values of radio button which is checked that values are storing hidden fields using javascript. But those values are reflecting back to code behind when click the button. Only the last value is sending to code behind that i don't require. I required all the values from javascript to code behind when i click the finish button. how to achieve this
Prifti Constantine 2-Jan-19 4:13am    
If the radioButton has the tag "runat='server'" then you can simple get the control value in the code behind by typping its id.
if you have a <asp:HiddenField runat="server" ID="HiddenControlName" /> or a radiobutton , Control Types do not matter, if the runat=server tag is included then you can use string hiddenValue = HiddenControlName.value.ToString();

Either that or some more code is needed for the solution of your problem.
Member 8583441 2-Jan-19 4:25am    
yes sir but this radio buttons are taken from string builder which does not require runat=server because i have used HTML input type radio
A) {2}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900