How to assign a value to hidden field using javascript in asp.net

asp.net , java , javascript United Arab Emirates
  • 5 years ago

    Hi there,

    <input id="HiddenField1" type="hidden" value="" runat="server" />

    protected void Page_Load(object sender, EventArgs e)
    {
    HiddenField1.Value = "1";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.Write(HiddenField1.Value);
    }
  • 5 years ago

     You are going to need to use an ID with your hidden input field. In the example below I added a value to the hidden input field and then called it again and put it into an alert to display it.

     

    <head runat="server">

    <title>Untitled Page</title>

     

    <script type="text/javascript">

     

    function SetValue()

    {

    document.getElementById(
    'Hidden1').value = "testing !!!!!";

     

    alert(document.getElementById(
    'Hidden1').value);

    }

     

    </script>

     

    </head>

    <body onload="SetValue();">

    <form id="form1" runat="server">

    <div>

    <input id="Hidden1" type="hidden" value="" />

    </div>

    </form>

  • 5 years ago

    I see your problem. You know that ASP is server side code and javascript is client side code. You can not access HTML controls with ASP and you can not access ASP controls with Javascript. But since ASP does output HTML to the browser, once it does that then you can access it with Javascript. So if you use a ASP hidden control you can set the value in your codebehind then you can access it with javascript from the HTML.

    <asp:HiddenField ID="HiddenField1" runat="server" Value="" />

    CodeBehind

    protected void Page_Load(object sender, EventArgs e)

    {

     if (!Page.IsPostBack)

    {

    HiddenField1.Value =
    "testing";

    }

    Now you can use javascript on it. You can get it value with javascript, you can change its value to something else and with a post back ASP can get the new value that you inputted with javascript. 

     function SetValue()

    {

    alert(document.getElementById('HiddenField1').value);

    }

    Hope this helps!

  • 4 years ago
    HI Devi, I'm a newbie in asp.net.Please can u help me in solving the same problem i'm facing.I'm not able to access the hidden variable values and also the error comes as object required when i try to access. Please help me to solve this problem. I dont hav master page concept in my application.Does it make any difference Thanks and Regards Kris
  • 4 years ago

    Its been 11 days since you have posted so you may have already solved your problem. But if you have not, from the error you say you are getting "object required" it sounds like you are using a html hidden control and not a asp hidden control.

    If thats not you problem you may want to post the part of your code that is giving you the problem both asp and html.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The difference between theory and practice is smaller in theory than in practice.”