Use server controls to call the front-end client js method in the background

Source: Internet
Author: User

Today I tried to study the server controls to control JSCode(It can be seen that not only HTML controls can call JS methods, but server controls can also call JS methods ),

I think it is a little practical. I want to share the following:

The front-end code is as follows:

JS method: <script language = "JavaScript">
Function check ()
{
If (document. All ("checkbox1"). Checked = true)
{
Alert ("OK ");
// Document. getelementbyid ('textbox1'). style. Visibility = "hidden ";
Document. All ('textbox1'). style. Display = "NONE ";// Both methods can control the display and hiding of textbox.
}
Else
{
// Document. getelementbyid ('textbox1'). style. Visibility = "visible ";
Document. All ('textbox1'). style. Display = "Block ";// Both methods can control the display and hiding of textbox.
}
}
</SCRIPT>

A textbox and a checkbox.You can click checkbox to control the display and hide of textbox.

<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: checkbox id = "checkbox1" runat = "server"/>
</Div>
</Form>
</Body>

Call the js method on the background page as follows:

Protected void page_load (Object sender, eventargs E)
{
Checkbox1.attributes. Add ("onclick", "check ()");
}

 

 

Common client calls are as follows:

The js method remains unchanged, just change the control to an HTML control:

<Body>
<Form ID = "form1" runat = "server">
<Div>
& Nbsp; <input id = "textbox1" type = "text"/>
<Input id = "checkbox1" type = "checkbox"Onclick = "check ()"/>

</Div>
</Form>
</Body>

 

There are two other methods,

(1) Add the onclick event directly behind the server control without writing code in the background. Also, as follows:

<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: checkbox id = "checkbox1" runat = "server"Onclick = "check ()"/>

(2) drag two HTML controls in the toolbox, right-click them, and select "run as a server control" as follows:

<Input id = "textbox1" type = "text"Runat = "server"/>
<Input id = "checkbox1" type = "checkbox"Runat = "server"/>

Then, call the js method on the background page as follows:

Protected void page_load (Object sender, eventargs E)
{
Checkbox1.attributes. Add ("onclick", "check ()");
}

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.