Change javascript variable value from code

I have this javascript code:

<script> $(document).ready(function() { var progValue1 = 100; var progValue2 = 30; $("#progressbar").progressbar({ value: progValue1}); $("#progressbar2").progressbar({ value: progValue2 }); }); </script> 

I would like to change the values ​​of two variables ( progValue1 and progValue2 ) from the code that progValue2 when the button is clicked.

This is the button code

 <asp:Button ID="btnConfirm" CssClass="button" SkinID="Common" runat="server" Text= "Confirm" OnClick="btnConfirm_Click" /> 

How can I change these values ​​from C # code for the btnConfirm method?

+7
source share
2 answers

Add two properties, i.e.

 private int _progValue1 = 100; private int _progValue2 = 30; protected int ProgValue1 { get { return this._progValue1; }} protected int ProgValue2 { get { return this._progValue2; }} 

Change your JS:

 <script> $(document).ready(function() { var progValue1 = <%=ProgValue1%>; var progValue2 = <%=ProgValue2%>; $("#progressbar").progressbar({ value: progValue1}); $("#progressbar2").progressbar({ value: progValue2 }); }); </script> 

Then set the values ​​in the OnClick event handler:

 this._progValue1 = 40; this._progValue2 = 20; 
+9
source

What I assume from your request is that you want to change the value of the Javascript variable from the code behind, you want to send the value from the code to javascript. You can use ScriptManager.RegisterClientScriptBlock for this. For more elobrate, see the code below:

Js

 function ChangeValue(value1) { yourvariable = value1; } 

Code for

 private void SomeMethod() { string newvalue = "test value"; //need to pass this to JS var ScriptManager.RegisterClientScriptBlock(this,typeof(Page),"key","ChangeValue("+ newvalue +");",true); } 
+1
source

All Articles