很多时候,我们使用服务器端控件写出的代码,会给我们生成一些很多我们看不懂的代码(初学者),但是有时候我们并不需要这些代码(业务需求不同),对于生成的一些代码感到多余。所以我就开始想,有没有一种可能:不使用服务器端控件(包括form表单不加runat="server"属性)来触发后台写的某一个方法或事件(ASP.NET的事件实际上是使用事件机制来驱动的)。经过测试是可以的。

原理:使用反射驱动方法。

   步骤:

     1、手写一个提交表单的js函数(可以使用asp.net的__dopostBack函数来驱动)

   2、使用表单提交到当前这个aspx页面

   3、使用反射机制来调用指定方法

  aspx页面的代码:

  

<%@ 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>
</head>
<body>
<form action="WebForm1.aspx" method="post" id="form1">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input language="javascript" onclick="__doPostBack('Button2','2')" id="Button2" type="button"
value="Button2" />
<input language="javascript" onclick="__doPostBack('Button1','1')" id="Button1" type="button"
value="Button1" />
<input language="javascript" onclick="__doPostBack('Button3','1')" id="Button3" type="button"
value="Button3" />
<a href="#" onclick="__doPostBack('LinkButton1','1')">这是LinkButton(模拟)</a>
</form>
</body>
</html>

  

  aspx.cs文件的代码

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()
{
//取得MyClass的Type对象,下面的代码使用Type的静态方法需指明程序集,作用相同
//Type t = Type.GetType("Mengliao.CSharp.C13.S02.MyClass, ConsoleApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
Type t = typeof(MyClass);
//通过Activator实例化MyClass,供实例方法调用
object obj = Activator.CreateInstance(t, new object[] { }); //MethodInfo[] methods = t.GetMethods(); //获取MyClass的所有方法列表 //foreach (MethodInfo nextMethod in methods) //枚举所有方法
//{
// Console.WriteLine(nextMethod.ToString()); //显示方法信息
// if (nextMethod.Name == "m1") //方法m1
// {
// nextMethod.Invoke(obj, null); //使用obj对象调用方法m1,无参数
// }
// if (nextMethod.Name == "m2") //方法m2
// {
// //静态方法,使用null调用方法m2,建立参数数组,传入10
// Console.WriteLine("Called static method 2, return {0}", nextMethod.Invoke(null, new object[] { 10 }));
// }
//} MethodInfo m3Info = t.GetMethod("Get"); //获取方法m3
m3Info.Invoke(obj, null); //调用方法m3,传入对应的2个参数 //获取方法m4,使用obj对象调用方法,无参数
//t.InvokeMember("m4", BindingFlags.InvokeMethod, null, obj, null); ////建立泛型委托runMe,并绑定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)); //调用该委托 //Console.ReadLine(); } public string Invoke(string Method)
{ Type t = typeof(MyClass);
//通过Activator实例化MyClass,供实例方法调用
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 m3Info = t.GetMethod(Method); //获取方法m3
m3Info.Invoke(obj, null); //调用方法m3,传入对应的2个参数
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;
} } }

  MyClass.cs文件里面就是事件(方法)

  

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 = "";
string str2 = "";
} public void Button1()
{
string str1 = "";
string str2 = "";
} public string Button3()
{
return "hello world";
} public string LinkButton1()
{
return "你好";
}
}
}

不使用ASP.NET服务器端控件(包括form表单不加runat="server")来触发.cs里的事件(方法),(适用于有代码洁癖者)。的更多相关文章

  1. asp.net动态增加服务器端控件并提交表单

    为什么要用原生的呢? 1.目的 原生出现浏览器兼容性问题 极少,不用测试多浏览兼容性 .需要考虑到市面上的其他垃圾浏览器. 2.性能不好 如果不考虑第一条 你可以换一种方式 直接上代码 .aspx页面 ...

  2. ASP.NET服务器端控件(class0617)

    ASP.Net服务端基本控件介绍 ASP.Net服务端控件是ASP.Net对HTML的封装,在C#代码中就可以用txt1.Text=‘abc’这种方式来修改input的值,ASP.Net会将服务端控件 ...

  3. javascript获取asp.net服务器端控件的值

    代码如下: <%@ Page Language="C#" CodeFile="A.aspx.cs" Inherits="OrderManage_ ...

  4. js如何获取asp.net服务器端控件的值(label,textbox,dropdownlist,radiobuttonlist等)

    js如何获取asp.net服务器端控件的值(label,textbox,dropdownlist,radiobuttonlist等) 欢迎访问原稿:http://hi.baidu.com/2wixia ...

  5. 037——VUE中表单控件处理之表单修饰符:lazy/number/trim

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. ASP.NET--Web服务器端控件和Html控件

    今天学习总结了一些相关概念和知识. 之前无论是做 单机的winform 还是 CS的winform 感觉,不到两年下来感觉还可以,虽然API有很多,但是还是比较熟悉基于WINDOWS消息机制的编程,但 ...

  7. ASP.NET服务器端控件原理分析

    服务器端控件触发事件分两种: 1.服务器端控件Button被渲染成客户端的 <input type="submit" name="Button1" val ...

  8. WebForm 【简单控件】【表单元素】

    一.HTML 表单元素复习 (1)文本类 文本框:<input type="text" name="" id="" value=&qu ...

  9. JS触发ASP.NET服务器端控件的方法

    <asp:Button ID="Button_regId" runat="server" Font-Bold="False" OnCl ...

随机推荐

  1. 【转】在ubuntu环境下搭建svn server遇到的一些问题

    原文网址:http://www.cnblogs.com/pcchinadreamfly/archive/2012/11/24/2786046.html 前段时间在ubuntu 12.04lts上倒腾了 ...

  2. 使用VisualStudio进行单元测试之四 顺序测试

    前文中所提到的测试都是针对一个方法进行的独立测试,即使是同事测试多个方法,他们之间也没有影响.但是在实际的生产过程中,更多的情况是方法与方法之间是存在相互的逻辑关系的,所以也就有了今天要介绍的顺序测试 ...

  3. VS2010编译libjpeg

    环境:win7旗舰版 x64 VS2010 下载源代码下载地址:http://www.ijg.org/,选择windows format file 解压源代码,修改源代码中jconfig.vc为jco ...

  4. UVA 11762 Race to 1(记忆化+期望)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20869 [思路] DP+期望. 设f[x]表示从x转移到1的期望操 ...

  5. int 和 long的区别

    数据模型决定了C语言中基本数据类型与数据宽度的对应关系.我们常用的32位模型称为ILP32,而64位模型有三种:LP64.LLP64和ILP64.在64位模型中,指针一定是64位的,但是int和lon ...

  6. Transaction: atomicity, consistency, separability, persistence

    Transaction: atomicity, consistency, separability, persistence http://langgufu.iteye.com/blog/143440 ...

  7. 不同车型 不同起步价不同费用的问题(switch语句)

    #include "stdio.h" #include "math.h" main() { int a; float meet,b; printf(" ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(21)-权限管理系统-跑通整个系统

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(21)-权限管理系统-跑通整个系统 这一节我们来跑通整个系统,验证的流程,通过AOP切入方式,在访问方法之 ...

  9. android 55

    智能家居:可以联网可以用指令操作可以返回状态.智能微波炉智能眼镜智能手表. Android作者Andy Rubin,2007年正式由谷歌推广,开源的. 安卓体系结构和开发一个闹钟的调用结构图: 安卓和 ...

  10. Qss

    *{ font-size:13px; color:white; font-family:"宋体"; } CallWidget QLineEdit#telEdt { font-siz ...