How do you pass variables from c# to javascript?

后端 未结 5 1720
無奈伤痛
無奈伤痛 2020-11-29 08:20

Looking to pass variables from c# to javascript to use some jquery code. Passing doubles, ints, strings, arrays. Does anyone know how to do this?

for example if I ha

5条回答
  •  萌比男神i
    2020-11-29 09:10

    You can use public properties on the code behind as well as the session. Here is a sample using public properties that can be read from a Javascript function.

    Front End:

    function IsAgentInProgram()
    {
        var optStatus = "<%=AgentOptInStatus%>";
    
        if (optStatus == "True")
            alert("You are opted in!");
        else
            alert ("You are opted OUT");
    }
    

    Code Behind:

    public bool AgentOptInStatus;
    
    private void Page_Load(object sender, System.EventArgs e)
    {
        this.AgentOptInStatus = true;
    
    }
    

提交回复
热议问题