Community discussion forum

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

  • 1 year ago

    Hi,

       I am trying to assign a value to a hidden field by using the following code but its not working.So can anyone give some better suggestion.

    function hidval(frm,val)

    {

      frm.hidden1.value=val

    }

    <body onload="BLOCKED SCRIPThidval(this.form,<%=value%>)">

    <input type="hidden" name="hidden1" value="">

     /// In the code Behind I am calling the hidden value as follows

    dim var = request.form("hidden1")

    but the above code is not working so please help me to solve this issue.

  • 1 year 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);
    }
  • 1 year ago

     

    Hi, In the code behind I am not getting Hiddenfield's Value by inheriting its property.So its not working ya.

     

     

  • 1 year 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>

  • 1 year ago

    Hi,

      Thank You So Much For Your Reply Ya.In The Above Code You Are Assigning Value To a Hidden Field Inside a JavaScript, Thats Fine.But The Problem Is I Need To Pass a Parameters From The Code Behind File To The JavaScript Function.So How Can I Do It?

  • 1 year 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!

  • 1 year ago

     

    Hi,

       With Help Of Your Above Code, I Have Solved My Problem.

    Thank You So Much Ya.

     

    Regards,

    Devi.C 

     

  • 6 months 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
  • 6 months 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).

We'd love to hear what you think! Submit ideas or give us feedback