0

Can't get value from getValue in JS and can't show value in showValue field.

Thanks in advance

ASPX PAGE CODE

<script>
var x = document.getElementById("<%=getValue.ClientID %>").innerHTML;    
document.getElementById('showValue').innerHTML = x;
</script>

<asp:Label runat="server" ID="getValue" />
<asp:Label runat="server" ID="showValue" />

C# CODE

getValue.Text = "value from code";
2
  • Any errors? What happens? Commented Aug 14, 2014 at 14:46
  • no error. just can't get value. Commented Aug 16, 2014 at 5:47

1 Answer 1

1

You need to call javascript once all the controls are rendered, then only you can get the proper id of the <asp:label> control, so you need to write that javascript code under window.onload().

Also for showValue, you need to use ClientID.

window.onload= function(){
   var x = document.getElementById("<%=getValue.ClientID%>").innerHTML;
   document.getElementById('<%=showValue.ClientID %>').innerHTML = x;
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @w3hunter .. its working fine. if i want to get JS variable x value from code behind, how can i do this ?
the value of x will be the text value of getValue control, so you should be able to access it using the Text property of getValue control, using "getValue.Text". Hope it answers your question. Btw, if you want to access javascript variable in CodeBehind, you can achieve it using hidden variables. You will get more info here forums.asp.net/p/1211504/2139016.aspx#2139016.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.