从后面的代码更改Javascript变量值

时间:2012-08-01 09:56:57

标签: c# asp.net progress-bar

我有这个javascript代码:

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

我想在点击按钮时从后面的代码中更改两个变量(progValue1progValue2)的值.....

这是按钮的代码

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

如何从btnConfirm方法的C#代码更改这些值?

3 个答案:

答案 0 :(得分:9)

添加两个属性,即

private int _progValue1 = 100;
private int _progValue2 = 30;

protected int ProgValue1 { get { return this._progValue1; }}
protected int ProgValue2 { get { return this._progValue2; }}

修改你的JS:

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

然后在OnClick事件处理程序中设置值:

this._progValue1 = 40;
this._progValue2 = 20;

答案 1 :(得分:1)

我从您的查询中假设您想要从代码隐藏更改Javascript变量的值意味着,您希望将代码后面的值发送到javascript。您可以使用ScriptManager.RegisterClientScriptBlock来实现此目的。为了更加高兴,请参阅以下代码:

JS

function ChangeValue(value1)
{
   yourvariable = value1;
}

背后的代码

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

答案 2 :(得分:0)

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


private int _progValue1 = 1;

    protected int ProgValue1
    {
        get
        {
            return _progValue1;
        }
    }