Community discussion forum

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

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

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 6 months 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);
    }
  • 6 months ago

     

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

     

     

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

  • 6 months 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?

  • 6 months 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!

  • 6 months ago

     

    Hi,

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

    Thank You So Much Ya.

     

    Regards,

    Devi.C 

     

Post a reply

Enter your message below

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