The technology used for web development is aspx.net. However, it is quite awkward to get used to ASP. The reason is that there are many functions that can be processed at the front end, however, due to the use of a lot of webcontrol, continuous PostBack occurs. It may be better if Ajax is used for processing. Currently, you can only bind front-end JavaScript Functions to these controls in the background to support front-end processing functions. I found it online and found several methods to meet the requirements:
First, it feels the most convenient to use
Button1.attributes. Add ("onclick", "Return onbtnclick ()");
"Onbtnclick ()" is a foreground method in which data is processed directly at the foreground. Can be replaced with a general script
Second, onclientclick
<Asp: button id = "button1" runat = "server" text = "button" onclientclick = "onclientclick ()" onclick = "button#click"/>
Button1_click is the processing method in CS (background), while onclientclick () is the processing function of JavaScript (foreground.
Third, dynamically add scripts using the clientscript class
The usage is as follows: add where you want to call a Javascript script FunctionCodeMake sure that myfun has been defined in the script file.
Clientscript. registerstartupscript (clientscript. GetType (), "myscript", "<SCRIPT> myfun (); </SCRIPT> ");
This method is more convenient than response. Write. You can directly call the UDF in the script file.
Fourth, clientscript. registerstartupscript
Example: stringbuilder sb = new stringbuilder ();
SB. append ("<script language = 'javascript '> ");
SB. append ("button2_onclick ('" + serverpath + "')");
SB. append ("</SCRIPT> ");
Clientscript. registerstartupscript (this. GetType (), "loadpicscript", SB. tostring ());
Fifth, write the script using the response. Write method.
For example, after you click the button, you must first operate the database. After the operation is complete, it is displayed that the operation is complete. You can write it in the last place you want to call.
Response. Write ("<SCRIPT type = 'text/JavaScript '> alert (); </SCRIPT> ");
This method has a defect that the user-defined functions in the script file cannot be called, and only internal functions can be called. The specific user-defined functions can only be called in response. write the function definition in write, such as response. write ("<SCRIPT type = 'text/JavaScript '> function myfun (){...} </SCRIPT> ");
This method can be used to bind a front-end method from the background, or bind a front-end method from the CPP language environment.