Recently, I am working on a submodule for contribution to an academic journal, which involves the hiding and display of Divs, specifically, you need to obtain data in the background and then decide whether to hide or display the DIV, select the checkbox, or unselect the status.
I think a lot of self-righteous methods and fail one after another.
The following provides the correct ideas and methods, which can be used to accumulate something for yourself.
1. Front-end code
Code
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> test </title>
<Style type = "text/CSS">
# Div_box
{
Width: 300px;
Border: solid 1px red;
}
</Style>
<SCRIPT type = "text/JavaScript">
Function show ()
{
VaR cbox = Document. getelementbyid ('cb ');
VaR div_ B = Document. getelementbyid ('div _ box ');
If (cbox. Checked)
{
Div_ B .style.display = "Block ";
}
Else
{
Div_ B .style.display = "NONE ";
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
Option 1 ............ <Br/>
<Br/>
Option 2 ............ <Br/>
<Br/>
.............................. <Br/>
<Br/>
<Input type = "checkbox" id = "CB" runat = "server" onclick = "show ()"/> <label for = "CB"> additional options </label>
</Div>
<Div id = "div_box" runat = "server">
Name of the second author: <asp: textbox id = "txt_authorname2" runat = "server"> </ASP: textbox> <br/>
<Br/>
Gender of the second author: <asp: textbox id = "txt_authorsex2" runat = "server"> </ASP: textbox> <br/>
<Br/>
Author 2: <asp: textbox id = "txt_authorage2" runat = "server"> </ASP: textbox>
</Div>
</Form>
</Body>
</Html>
2. Background code
Code
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
// Obtain the second author's information ............
// Set CB. Checked = true or false
If (CB. Checked)
{
// Div_box.visible = true; // This write method is acceptable, but when the JS script (document. if getelementbyid ('div _ box') is null), the error "Object missing" appears.
Div_box.attributes.add ("style", "display: Block ");
}
Else
{
// Div_box.visible = false; // same as above
Div_box.attributes.add ("style", "display: none ");
}
}
}
}
The above code has two points to note: one is the background code and the two sentences commented out. I thought this was the case. Later I found that when I click the checkbox button, a script error occurs, saying "the object is missing ".. In addition, the div_box style should be written in a style sheet or embedded between Div_box.attributes.add ("style", "display: block; width: 300px; Border: solid 1px red ");
Div_box.attributes.add ("style", "display: none; width: 300px; Border: solid 1px red ");
But to be honest, this is not a good method.
Remember later .....