Server controls and JavaScript

Source: Internet
Author: User

Server controls and JavaScript
We use js to access the server button control,
Function aspbuttonclick () {document. form1.button1. value = "I Have Been clicked ";}
This section of JS is placed in <Form ID = "form2" runat = "server">
<Div>
<Asp: button id = "butt1on1" runat = "server" text = "I am aspbutton" onclick = "aspbuttonclick ()"/>
</Div>
</Form>
In this case, the following error is returned:
Cs0117: "ASP. default13_aspx" does not contain the definition of "aspbuttonclick"
Why is this error? In my opinion, this button is run on the server and JS is run on the browser, so the aspbuttonclick function cannot be found.
1. if <asp: button id = "butt1on1" runat = "server" text = "I am aspbutton" onclick = "aspbuttonclick () "/> change to <input type =" button "id =" button1 "runat =" server "value =" I Am a server button "onclick =" aspbuttonclick () "/>
You can run the command correctly. Note that the runat = "sever"; <input type = "button"/> can be correctly run without adding.
If you double-click the input. the CS file generates the event protected void button1_serverclick (Object sender, eventargs e) {}. I will do this in it, response. write ("Hello, Web ");
However, there will be two click events, although it can be understood as one is the client Click Event, the other is the server click event,
However, the results are confirmed that neither event can be executed normally. You only need to remove one of the two to perform the operation correctly.
2. If we remove the click event, set <script language = "JavaScript" type = "text/JavaScript"> document. form1.button1. value = "I clicked ";
</SCRIPT>
Put it behind <asp: button id = "butt1on1" runat = "server" text = "I am aspbutton"/>, Code You can run the command correctly.
For the ASP: button control, you can also click the event (Note: View-> the location of the JavaScript code in the source file)
Protected void page_load (Object sender, eventargs E)
{
Button1.attributes. Add ("onclick ",
"Javascript: Alert ('pay more attention !!! ')");
} Or
Page. clientscript. registerstartupscript (this. GetType (), "myscript ",
"Function alerthello () {alert ('hello, ASP. net') ;}", true );
Button1.attributes ["onclick"] = "alerthello ()";
Button2.attributes ["onclick"] = "alerthello ()";

Let's look at an example of msdn:
<Div>
<P>
<Asp: imagebutton id = "imagebutton1"
Onmouseover = "this.src+'button2.gif '"
Onclick = "imagebutton1_click"
Onmouseout = "this.src+'button1.gif '" runat = "server"
Imageurl = "button1.gif"> </ASP: imagebutton>
</P>
<P>
<Asp: Label id = "label1" runat = "server"/>
</P>
</Div>

Protected void page_load (Object sender, eventargs E)
{
Page. registerclientscriptblock ("myscript ",_
"If (document. Images) {" +
"Mybutton = new image;" +
"Mybuttonshaded = new image;" +
"Mybutton. src = 'button1.gif;" +
"Mybuttonshaded. src = 'button2.gif;" +
"}" +
"Else {" +
"Mybutton =''; "+
"Mybuttonshaded =''; "+
"}", True );

Imagebutton1.attributes. Add ("onmouseover ",
"This. src = mybuttonshaded. SRC;" +
"Window. Status = 'Yes! Click here! ';");
Imagebutton1.attributes. Add ("onmouseout ",
"This. src = mybutton. SRC;" +
"Window. Status = '';");
}
 
Protected void imagebutton1_click (Object sender, imageclickeventargs E)
{
Label1.text = "resend! ";
}

-->
<! --
Let's take a look at the example of the JS Control Server Control checkboxlist.
Checkboxlist generated by the client
<Div>
<Table id = "Table1" border = "0">
<Tr>
<TD> <input id = "checkboxlist00000" type = "checkbox" name = "checkboxlist1 $0"/> <label for = "checkboxlist00000"> 1232 </label> </TD>
</Tr> <tr>
<TD> <input id = "checkboxlist1_1" type = "checkbox" name = "checkboxlist1 $1"/> <label for = "checkboxlist1_1"> 254 </label> </TD>
</Tr> <tr>
<TD> <input id = "checkboxlist1_2" type = "checkbox" name = "checkboxlist1 $2"/> <label for = "checkboxlist1_2"> 5643 </label> </TD>
</Tr> <tr>
<TD> <input id = "checkboxlist1_3" type = "checkbox" name = "checkboxlist1 $3"/> <label for = "checkboxlist1_3"> 789 </label> </TD>
</Tr> <tr>
<TD> <input id = "checkboxlist1_4" type = "checkbox" name = "checkboxlist1 $4"/> <label for = "checkboxlist1_4"> 654 </label> </TD>
</Tr> <tr>
<TD> <input id = "checkboxlist00005" type = "checkbox" name = "checkboxlist1 $5"/> <label for = "checkboxlist00005"> 564 </label> </TD>
</Tr> <tr>
<TD> <input id = "checkboxlist00006" type = "checkbox" name = "checkboxlist1 $6"/> <label for = "checkboxlist00006"> 8564 </label> </TD>
</Tr> <tr>
<TD> <input id = "checkboxlist00007" type = "checkbox" name = "checkboxlist1 $7"/> <label for = "checkboxlist00007"> 8564 </label> </TD>
</Tr> <tr>
<TD> <input id = "checkboxlist00008" type = "checkbox" name = "checkboxlist1 $8"/> <label for = "checkboxlist00008"> 5452 </label> </TD>
</Tr> <tr>
<TD> <input id = "checkboxlist00009" type = "checkbox" name = "checkboxlist1 $9"/> <label for = "checkboxlist00009"> 5641 </label> </TD>
</Tr>
</Table>
</Div>
-->

<Head runat = "server">
<Title> No title page </title>
<Script language = "JavaScript" type = "text/JavaScript">
Function checkall ()
{
// Alert (document. getelementbyid ("checkboxlist1"). getelementsbytagname ("input"). Length );
For (VAR I = 0; I <document. getelementbyid ("checkboxlist1"). getelementsbytagname ("input"). length; I ++)
{
Document. getelementbyid ("checkboxlist1 _" + I). Checked = true;
}
}
Function deleteall ()
{
For (VAR I = 0; I <document. getelementbyid ("checkboxlist1"). getelementsbytagname ("input"). length; I ++)
{
Document. getelementbyid ("checkboxlist1 _" + I). Checked = false;
}
}
Function reverseall ()
{
For (VAR I = 0; I <document. getelementbyid ("checkboxlist1"). getelementsbytagname ("input"). length; I ++)
{
VaR objcheck = Document. getelementbyid ("checkboxlist1 _" + I );
If (objcheck. Checked)
Objcheck. Checked = false;
Else objcheck. Checked = true;
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: checkboxlist id = "checkboxlist1" runat = "server">
<Asp: listitem> 1232 </ASP: listitem>
<Asp: listitem> 254 </ASP: listitem>
<Asp: listitem value = "5643"> 5643 </ASP: listitem>
<Asp: listitem> 789 </ASP: listitem>
<Asp: listitem> 654 </ASP: listitem>
<Asp: listitem> 564 </ASP: listitem>
<Asp: listitem> 8564 </ASP: listitem>
<Asp: listitem> 8564 </ASP: listitem>
<Asp: listitem> 5452 </ASP: listitem>
<Asp: listitem> 5641 </ASP: listitem>
</ASP: checkboxlist>
</Div>
<Div>
<Input type = "button" onclick = "checkall ()" value = "select all"/>
<Input type = "button" onclick = "deleteall ()" value = "cancel"/>
<Input type = "button" onclick = "reverseall ()" value = "Invert selection"/>
</Div>
</Form>
</Body>
</Html>

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.