ASP.NET passes variables to Javascript in two implementations

  • 2020-05-19 04:28:38
  • OfStack

Method 1:
Use a hidden control, give the value of the variable to the hidden control, and then use Javascript to find the value of the hidden control
window.document.getElementById (" ID of the hidden control ").value // this will give you the value of the hidden control
Method 2:
javascript:
var value= < %=Num% >
Num is one property
Such as:
 
Public String Num 
{ 
set{...} 
get{...} 
} 

If the character variable is a character like alert() or something like that you use alert(" < %=Num% > ");
And Num1 must be declared as public

Interaction between javascript and asp.net
Recently, I was working on a background statistics system for web. I originally intended to use asp.net (code binding) for all of them, but I found it very difficult. Some functions were easier to use javascript. Many of them involve the interaction between C and S, as well as database operations. javascript is strong in the client control interface, but it is better to operate the database on the server as asp.net.
The js function is bound to the asp.net control:
The.Attributes.Add (event, function) control is fine, but I always report an error when adding the OnCheckedChanged event to the CheckBox.
Access the html control in asp.net:
Under 1 gas, replace all controls with html, and then add runat=server to the control so that it can be referenced in asp.net. Use (FindControl(control id) as html control type). Property, such as (this.FindControl ("ddlMonth") as HtmlSelect).SelectedIndex...
Read the variable asp.net in the html web page
In order to be able to read asp.net in js, make sure that js can distinguish the types of variables in asp.net.
Second, declare the variable as Public.
Use the variable "=" in js < %#asp.net variable name % > "; Can get the value of the variable, such as var id =" < %#userid% > "; (asp.net Public string userid = "zhangsan";
Being able to use js and asp.net in a good combination, give play to their respective strengths, and use them in a targeted way in the development process, can really get things done!

Related articles: