简体   繁体   中英

Hidden field value updated but not showing updated value in codebehind?

I am setting the hidden filed value through JavaScript as below

 <script lang="JavaScript" type="text/javascript">
 function ChangeVal()
 {
   var elem = document.getElementById("btnDownloadStream");
   if (elem.value == "Start")
   {
     elem.value = "Stop";
     document.getElementById('myHiddenInput').value = "1";
   }
   else
   {
     document.getElementById('myHiddenInput').value = "0";
     elem.value = "Start";
   }
 }

I am trying to get hidden field value in code behind. My code is

HiddenField myHiddenInput = (HiddenField)Page.FindControl("myHiddenInput");
var val = myHiddenInput.Value;

Before this line I am calling one function which creates and generates the GetResponseStream() . While doing this I am not able to get the value from server controls. Why?

确保您的隐藏字段具有runat="server"属性..

You forget about ViewState . If you change data in the hidden field using java script code the ViewState do not get this changes and that's why you cannot get the correct value in code behind.

Set Runat="server" attribute to your hidden field as shown below :

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

Then update your javascript function as shown below :

function ChangeVal() {
            var elem = document.getElementById("btnDownloadStream");
            if (elem.value == "Start") {
                elem.value = "Stop";
                document.getElementById('<%=myHiddenInput.ClientID%>').value = "1";
            }
            else {
                document.getElementById('<%=myHiddenInput.ClientID%>').value = "0";
                elem.value = "Start";
            }
        }

now you can directly access your hidden field value in your code behind without using Page.FindControl as mentioned :

var val = this.myHiddenInput.Value;

Update:

One thing I have noticed that your button is server side button and in your javascript you call

var elem = document.getElementById("btnDownloadStream");

I think it should be

var elem = document.getElementById('<%=btnDownloadStream.ClientId%>')

otherwise you will always get the value of else part

Make sure this is not the case.

Becuase Changing the value in javascript will not affect the server side value.

if you want to change a server side value from javascript: You can try the following

// Javascript
var myHidden = document.getElementById("<%:myHiddenId.ClientId%>");
myHidden.value = myJSVariable;

Make sure the myHidden is a server control.

This also happens in case you use the UpdatePannel in your page. if this is the case then place the HiddenField inside the UpdatePannel and try again.

well @Vishweshwar Kapse is answered your question.

Place your hidden field in update panel and don't forget to add click event of button in trigger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM