How to access JS variable in C#

前端 未结 5 1451
轻奢々
轻奢々 2021-01-15 19:33

Let\'s say I have

500

(Context: asp.net aspx page) How do I allow a c# code access that value?

<
5条回答
  •  渐次进展
    2021-01-15 20:01

    Use a hidden input box

       
    

    Put your value in the input box with javascript

    //call this method before you postback, maybe on form submit
    function SetPostbackValues() {
      var hiddenControl = '<%= hiddenControl.ClientID %>';
      document.getElementById(hiddenControl).value = 500; 
      //or some other code to get your value
    }
    

    Access the value with the input box on the server side

    protected void btnSubmit_Click(object sender, EventArgs e) {
       var hiddenValue = hiddenControl.Value;
    }
    

提交回复
热议问题