Do not use ASP. NET server controls (including form without runat = "server") to trigger events (methods) in. cs (applicable to code scammers ).,
Most of the time, the code written using the server control will generate a lot of code that we don't understand (for beginners), but sometimes we don't need this code (different business needs ), the generated code is redundant. So I started to think, is there a possibility: Do not use server-side controls (including form without the runat = "server" attribute) to trigger a method or event (ASP. NET events are actually driven by the event mechanism ). It is acceptable after testing.
Principle: Use the reflection-driven method.
Steps:
1. Write a js function for submitting a form (it can be driven by the _ dopostBack function of asp.net)
2. Use the form to submit to the current aspx page
3. Use the reflection mechanism to call the specified Method
Code of the aspx page:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm1.aspx. cs" Inherits = "Test. WebForm1" %> <! 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> </title> <script type =" text/javascript "> // <! [CDATA [function _ doPostBack (eventTarget, eventArgument) {var theForm = document. forms ['form1 ']; if (! TheForm) {theForm = document. form1;} if (! TheForm. onsubmit | (theForm. onsubmit ()! = False) {theForm. _ EVENTTARGET. value = eventTarget; theForm. _ EVENTARGUMENT. value = eventArgument; theForm. submit ();}} //]> </script>
Aspx. cs File Code
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using System. reflection; using System. web. script. serialization; namespace Test {public partial class WebForm1: System. web. UI. page {public WebForm1 () {} private string EventTarget {get {return Request. form ["_ EVENTTARGET"] ;}} private string EventTargument {get {Return Request. form ["_ EVENTARGUMENT"];} private new bool IsPostBack {get {if (string. isNullOrEmpty (EventTarget) & string. isNullOrEmpty (EventTargument) {return false;} return true ;}} protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {} else {Invoke (EventTarget) ;}} public void ss () {// get the Type object of MyClass. The following code uses the static method of Type to specify the assembly, same role // Type t = Type. getType ("Mengliao. CSharp. c13.S02. myClass, ConsoleApplication, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null "); Type t = typeof (MyClass); // instantiate MyClass through Activator, for instance method call object obj = Activator. createInstance (t, new object [] {88}); // MethodInfo [] methods = t. getM Ethods (); // obtain the list of all methods of MyClass // foreach (MethodInfo nextMethod in methods) // enumerate all methods // {// Console. writeLine (nextMethod. toString (); // display method information // if (nextMethod. name = "m1") // method m1 // {// nextMethod. invoke (obj, null); // call method m1 using the obj object, no parameter //} // if (nextMethod. name = "m2") // method m2 // {// static method, call method m2 using null, create a parameter array, and input 10 // Console. writeLine ("Called static method 2, return {0}", nextMethod. invoke (n Ull, new object [] {10}); //} MethodInfo m3Info = t. getMethod ("Get"); // Get method m3 m3Info. invoke (obj, null); // call method m3, input two corresponding parameters // obtain method m4, use obj object to call method, no parameter // t. invokeMember ("m4", BindingFlags. invokeMethod, null, obj, null); // create a generic Delegate runMe and bind it to the static private method of MyClass m5 // Delegate runMe = Delegate. createDelegate (typeof (Func <double, string>), t, "m5"); // Console. writeLine ("Call delegate with m5: Sqrt (2) = {0} ", (Func <double, string>) runMe) (2); // call this delegate // Console. readLine ();} public string Invoke (string Method) {Type t = typeof (MyClass); // instantiate MyClass through Activator for the instance Method to call object obj = null; // Activator. createInstance (t) if (Session ["currentObj"] = null) {obj = Activator. createInstance (t); //, new object [] {88} Session ["currentObj"] = obj;} else {obj = Session ["currentObj"] as object ;} methodInfo m 3 Info = t. getMethod (Method); // obtain the Method m3 m3Info. invoke (obj, null); // call method m3 and input the corresponding two parameters string name = (string) t. invokeMember (Method, BindingFlags. invokeMethod | BindingFlags. public | BindingFlags. instance, null, obj, null); if (! String. isNullOrEmpty (name) {Response. write ("<script> alert ('" + name + "'); </script>");} return name ;}}}View Code
The MyClass. cs file contains events (methods)
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; namespace Test {public class MyClass {public void Button2 () {string str1 = "1"; string str2 = "2" ;}public void Button1 () {string str1 = "1"; string str2 = "2";} public string Button3 () {return "hello world";} public string LinkButton1 () {return "hello ";}}}View Code