7

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?

3
  • 1
    see this post stackoverflow.com/questions/3655766/… Commented Aug 1, 2012 at 9:58
  • @PranayRana i just marked the answer that worked for me... is that what you wanted to know? Commented Aug 1, 2012 at 10:27
  • @WaqarJanjua saw that post... it was helpful. Commented Aug 1, 2012 at 10:28

3 Answers 3

9

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;
Sign up to request clarification or add additional context in comments.

2 Comments

i changed this a bit to "set" otherwise it was giving me a error.... public int ProgValue1 { get { return this._progValue1; } set { _progValue1 = value; } } public int ProgValue2 { get { return this._progValue2; } set { _progValue2 = value; } }
@riznomdemha - sorry about that, I corrected my code but you can use setters as well. Same difference.
1

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

JS

function ChangeValue(value1)
{
   yourvariable = value1;
}

Code Behind

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

Comments

0
$(function () {
        var progValue1 = parseInt('<%=ProgValue1%>');
        $("#progressbar").progressbar({ value: progValue1 });
    });


private int _progValue1 = 1;

    protected int ProgValue1
    {
        get
        {
            return _progValue1;
        }
    }

Comments

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.