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 ()");
}