Use ASP. NET to pass variables to Javascript

Source: Internet
Author: User
Method 1:
Use a hidden control, give the value of the variable to the hidden control, and use JavaScript to find the value of the hidden control.
Please wait Doc ument. getelementbyid ("hidden Control ID"). Value // you can get the value of the hidden Control

Method 2:
In javascript:
VaR value = <% = num %>

Num is an attribute.
For example:
Public String num
{
Set {...}
Get {...}
}

Use alert ("<% = num %>") if the character variable is character-like alert ");
Num must be a public statement.

Interaction between JavaScript and Asp.net

Recently I was working on a Web back-end statistics system. I wanted to use Asp.net (CodeBinding), but found very difficult, some functions are easier to use JavaScript. In many cases, the interaction between C and S and database operations are involved. Javascript is relatively strong on the client control interface, but it is better to operate the database on the server than Asp.net.

The JS function is bound to the Asp.net control:
You can use the control. Attributes. Add (event, function), but I always report an error when I add the oncheckedchanged event to the checkbox.

Access the HTML control in Asp.net:
In one breath, all controls are replaced with HTML, and then runat = server is added to the control so that they can be referenced in Asp.net. Use (findcontrol (Control ID) as HTML control type). properties, such as (this. findcontrol ("ddlmonth") as htmlselect). selectedindex ......

Read the Asp.net variable from the HTML webpage.
In order to be able to read data in Asp.net in JS, we need to ensure that JS of the variable type in Asp.net can be distinguished. In fact, we can find a way to change it to string!
Second, declare the variable as public.
Use variable = "<% # Asp.net variable name %>"; in JS to get the variable value, such as VAR id = "<% # userid %> "; (Public String userid = "zhangsan" in Asp.net ";

We can use a good combination of JS and Asp.net to give full play to our respective strengths and use them in a targeted manner during the development process. We can really handle things!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.