VS2010 about the mixed use of HTML controls and server controls separately and the JS function

Source: Internet
Author: User

This document resolves the following issues:

1. How do I access HTML controls in a. cs file?

Add a property to the HTML control runat= "Server" to

2. How do I invoke the JS function in an HTML control?

In the HTML control, you can use the event binding JS function, such as onclick= "Showjs ()"

3. In the. aspx file js, how to call the. cs file custom variables or methods?

  Use the <%= variable or method%> call to

4. How do I invoke the JS function in the. cs file?

Pass the statement this. Page.ClientScript.RegisterStartupScript (this. Page.gettype (), "", "<script>js function Method </script>") call to

5. In the. aspx file, how do I access the ASP. JS server control?

Add the property clientid= "Btncilent" (name custom in double quotes) in the server control, accessed by document.getElementById (id.clientid%> of the <%= server control)

6. How do I access HTML controls in the. aspx file in JS?

Follow the JS syntax, accessed by document.getElementById ("HTML control ID")

7. In the. aspx file, how does the HTML control invoke a variable or method that is customized by the. cs file?

You need to add public before a custom variable or method, while removing its properties runat= "Server", and then using the <%= variable or method %> call

1-1.index.aspx, added the Button,textbox control, input control, where the Button control is bound to click event Btn_click,

1-2.index.aspx.cs, added the click event Btn_click, custom method Getstr, Note that theinput control added the property runat= "Server" to make it a server control, can be accessed in a. cs file, such as Ipt.value

1-3.F5, run debug

1-4. ClickButtonControlinputThe content changes as follows

2-1. Invoking the JS function on the input control

2-2. Successful Operation

3-1. In the JS function, get the method or variable defined in the. cs file, in the format: <%= method or variable%>

3-2. Successful Operation

4-1. In the. cs file, call the JS function method, Format: This.Page.ClientScript.RegisterStartupScript (this. Page.gettype (), "", "<script>js function Method;</script>");

4-2. Successful Operation

The 5-1.js function gets the ID of the server control TextBox, note that you need to add the property clientid= "TXT" in the TextBox control, otherwise you cannot get the format: document.getElementById (" <%=TextBox1.ClientID%> ");

5-2. Because the server control textbox is parsed by the browser and becomes an HTML control, the Input,clientid property guarantees that the input control has the ID "txt" so that JS can find its ID

5-3. Successful Operation

6-1.js gets the ID of the HTML control input, in the format: document.getElementById ("IPT");

6-2. Successful operation

7-1. In HTML control input, call the custom method in the. cs file, note: Theinput control needs to remove the attribute runat= "Server", Format: <%=getstr ()%>

7-2. Successful Operation

VS2010 about the mixed use of HTML controls and server controls separately and the JS function

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.