How do you pass variables from c# to javascript?

后端 未结 5 1696
無奈伤痛
無奈伤痛 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条回答
  • 2020-11-29 08:55

    I recently used this ToJson() extension method along with Page.RegisterClientScriptBlock Method to pass down a multi-level mapping object that populates three levels of cascaded dropdowns.

    0 讨论(0)
  • 2020-11-29 08:59

    Nevermind I think I figured it out. Assuming the code above, you can write in javascript:

    <script type="text/javascript"> var JavascriptBlah = '<%=blah%>'</script>
    

    This will pass the blah in c# to the JavascriptBlah on the client side. Then you can manipulate on client side.

    0 讨论(0)
  • 2020-11-29 09:06

    you can use asp:HiddenField variables to pass values between codebehind and js.. this accomplished with viewstate of page during postbacks...

    0 讨论(0)
  • 2020-11-29 09:08

    You need this(How Do I: Add JavaScript to An ASP.NET Page), I guess?

    0 讨论(0)
  • 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;
    
    }
    
    0 讨论(0)
提交回复
热议问题