Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. create hidden field with javascript and get its value in code behind on postback

create hidden field with javascript and get its value in code behind on postback

Scheduled Pinned Locked Moved ASP.NET
javascriptquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krisp
    wrote on last edited by
    #1

    I dont want to have to register a hidden field in code behind, i want to dynamically create the hidden field in javascript and then it should appear in the Request.Form if i had created it in javascript. But it is never there in code behind (IsPostBack == true). It use to work, and now it doesn't. Anyone see why or what im missing? //javascript HiddenValue("CurrencyID__VALUE", "CAD"); // called from other function function HiddenValue(id, value) { var NewHiddenControl = document.getElementById(id); if (NewHiddenControl == null) { NewHiddenControl = document.createElement("INPUT"); NewHiddenControl.type = "HIDDEN"; NewHiddenControl.id = id; NewHiddenControl.name = id; } if (value != null) NewHiddenControl.value = value; return NewHiddenControl.value; } // code behind if (IsPostBack && Request.Form["CurrencyID__VALUE"] != null && Request.Form["CurrencyID__VALUE"].Length > 0) CurrencyID.SetValue(Request.Form["CurrencyID__VALUE"]); Thanks.

    A 1 Reply Last reply
    0
    • K krisp

      I dont want to have to register a hidden field in code behind, i want to dynamically create the hidden field in javascript and then it should appear in the Request.Form if i had created it in javascript. But it is never there in code behind (IsPostBack == true). It use to work, and now it doesn't. Anyone see why or what im missing? //javascript HiddenValue("CurrencyID__VALUE", "CAD"); // called from other function function HiddenValue(id, value) { var NewHiddenControl = document.getElementById(id); if (NewHiddenControl == null) { NewHiddenControl = document.createElement("INPUT"); NewHiddenControl.type = "HIDDEN"; NewHiddenControl.id = id; NewHiddenControl.name = id; } if (value != null) NewHiddenControl.value = value; return NewHiddenControl.value; } // code behind if (IsPostBack && Request.Form["CurrencyID__VALUE"] != null && Request.Form["CurrencyID__VALUE"].Length > 0) CurrencyID.SetValue(Request.Form["CurrencyID__VALUE"]); Thanks.

      A Offline
      A Offline
      Andrew Quinn AUS
      wrote on last edited by
      #2

      Hi there, From what I can see with the code you've posted, the problem is that you've created the hidden element but it's NOT a child of the FORM. Hence on post-back the values won't be posted and hence not picked up by the server-end. Adding the following to the document.createElement part of the code should fix it...

      NewHiddenControl = document.createElement("INPUT");
      // ...
      // ...
      var oForm = document.getElementById("Form1");
      if (oForm) oForm.appendChild(NewHiddenControl);
      

      Hope this helps, Andy

      K 1 Reply Last reply
      0
      • A Andrew Quinn AUS

        Hi there, From what I can see with the code you've posted, the problem is that you've created the hidden element but it's NOT a child of the FORM. Hence on post-back the values won't be posted and hence not picked up by the server-end. Adding the following to the document.createElement part of the code should fix it...

        NewHiddenControl = document.createElement("INPUT");
        // ...
        // ...
        var oForm = document.getElementById("Form1");
        if (oForm) oForm.appendChild(NewHiddenControl);
        

        Hope this helps, Andy

        K Offline
        K Offline
        krisp
        wrote on last edited by
        #3

        Hey, thank a lot Andy. Works flawlessly now. Perfect, thanks again.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups