0

I have an issue in getting the hidden field values, which is been set in page load at code behind. Problem is when i try to get that set values in javascript its giving undefined or null. Not able to get the values which was set in page load at code behind.

//cs code is like this
protected async void Page_Load(object sender, EventArgs e)
{

HiddenField_alt_edit.Value = "[{"unitid":"3072","unit_nameeng":"BOTTLE","purchcost":"2.000","salrate":"4.000","avgcost":"2.000","factor":"2"},{"unitid":"3073","unit_nameeng":"PKT","purchcost":"10.000","salrate":"20.000","avgcost":"10.000","factor":"10"}]";

ClientScriptManager script = Page.ClientScript;
                            script.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type=text/javascript>addAlternativeRowWithData();</script>");

}
//aspx page declaration goes like this. Also i am using a master page.
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<asp:HiddenField ID="HiddenField_alt_edit" runat="server"  Value="i am on."/>

</asp:Content>
// javascript file code goes like this
function addAlternativeRowWithData(mode) 
{

    alert("test");
    var idvalue = $("#HiddenField_alt_edit").val();
    alert(idvalue);
    alert($nonconfli('#ContentPlaceHolder1_HiddenField_alt_edit').val());
    var myHidden = document.getElementById('<%= HiddenField_alt_edit.ClientID %>').value;
    alert(myHidden);
    var json_string = $nonconfli('#ContentPlaceHolder1_HiddenField_alt_edit').val();
    var arr_from_json = JSON.parse(json_string);
    alert("test 2");
    alert(arr_from_json);
}

2 Answers 2

0

The id generated at client side can differ as the Hidden field is server side control in your code as you have set runat="server".

There are two ways normally. Either make the ClientIDMode static or use the instance in form to get client side id.

Like:

<asp:HiddenField ID="HiddenField_alt_edit" ClientIDMode="static" runat="server"  
                 Value="i am on."/>

and then following should work:

var idvalue = $("#HiddenField_alt_edit.ClientID").val();

or in client side code you need to get id like below:

var idvalue = $("#<%= HiddenField_alt_edit.ClientID %>").val();
2
  • I tried your solution sir, but the result was same as which i was getting before. All values undefined. Will do one thing will create a fresh test page and check how this will work. Apr 23, 2020 at 6:27
  • What i understood from the scenario is that i am using the web form technology. So when i use the same javascript code in another page (but this aspx page uses another master page) so its working. so this error is happening from the master page which i have added. I now have no clew to what difference i should look in these master pages Apr 23, 2020 at 18:16
0

Change:
HiddenField_alt_edit.Value = [{"unitid":"3072","unit_nameeng":"BOTTLE","purchcost":"2.000","salrate":"4.000","avgcost":"2.000","factor":"2"},{"unitid":"3073","unit_nameeng":"PKT","purchcost":"10.000","salrate":"20.000","avgcost":"10.000","factor":"10"}]

To this:
HiddenField_alt_edit.Value = " [{'unitid':'3072','unit_nameeng':'BOTTLE','purchcost':'2.000','salrate':'4.000','avgcost':'2.000','factor':'2'},{'unitid':'3073','unit_nameeng':'PKT','purchcost':'10.000','salrate':'20.000','avgcost':'10.000','factor':'10'}]"

Then, add this:
<asp:HiddenField ID="HiddenField_alt_edit" runat="server" Value="i am on." ClientIDMode="Static" />

In you js, you have to put this:

const value = document.getElementById('HiddenField_alt_edit').value

const jsonString = JSON.stringify(value)
const json = JSON.parse(jsonString)

console.log(json)
<input hidden id="HiddenField_alt_edit" value="[{'unitid':'3072','unit_nameeng':'BOTTLE','purchcost':'2.000','salrate':'4.000','avgcost':'2.000','factor':'2'},{'unitid':'3073','unit_nameeng':'PKT','purchcost':'10.000','salrate':'20.000','avgcost':'10.000','factor':'10'}]">

5
  • I tried this also, but the result was same as which i was getting before. All values undefined. Apr 23, 2020 at 6:27
  • Inspect the input with the inspector and check if the value is set. I think the value is not getting set right. Apr 23, 2020 at 6:34
  • When i am inspecting the element its showing value. But what ever i do it doesn't comes to JS file. Apr 23, 2020 at 10:37
  • What i understood from the scenario is that i am using the web form technology. So when i use the same javascript code in another page (but this aspx page uses another master page) so its working. so this error is happening from the master page which i have added. I now have no clew to what difference i should look in these master pages – Apr 23, 2020 at 18:31
  • You can see de name of the section. When you use a MasterPage and the control doesn't have the property: ClientIDMode="Static". ASP set the id of the section before the real id. So, if you have id="hiddenValue" and the name of section is "bodySection" the control id becomes "bodySection_hiddenValue". Again, to avoid this you should put ClientIDMode="Static" in your control attributes. Apr 23, 2020 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.