Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a Javascript variable value from code behind

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 the two variables (progValue1 and progValue2) from the code behind when a button is clicked .....

This is the code for the button

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

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

like image 626
Monzir Avatar asked Aug 01 '12 09:08

Monzir


People also ask

How can access JavaScript variable in asp net code behind?

To get JavaScript values in the code-behind, we need Hidden Input Control, whose runat attribute we will set to runat="Server" so that it can be accessible in code-behind. It will work as an intermediately to pass the value from client-side to server-side.

How Pass value from JavaScript to code behind in VB net?

How to Pass Values from JavaScript to CodeBehind? You can also use a HTML INPUT control by setting runat="server". This is another way where we can make an Ajax call to server whenever we need the server value in javascript/jquery. When you use ASP.Net AJAX then you can use PageMethods for this.


1 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; }}

Modify 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 your OnClick event handler:

this._progValue1 = 40;
this._progValue2 = 20;
like image 84
Chris Gessler Avatar answered Nov 03 '22 00:11

Chris Gessler