反射调用事件

ljhkim6 2008-08-29 02:52:59
如何使用反射的方式在用户控件的tbx1_TextChanged事件里调用aspx页面里的button事件?
...全文
404 29 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
linghuxiaochong 2008-11-11
  • 打赏
  • 举报
回复
http://blog.csdn.net/linghuxiaochong/archive/2008/11/11/3275521.aspx
Black_Black_Heart 2008-08-29
  • 打赏
  • 举报
回复
mark
ljhkim6 2008-08-29
  • 打赏
  • 举报
回复
最终答案:
System.Web.UI.Page page = this.Page;
Button btn = page.FindControl("Button1") as Button;
Type t = btn.GetType();
MethodInfo mi = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(btn, new object[] { e });

谢谢ojlovecd

我姓区不姓区 2008-08-29
  • 打赏
  • 举报
回复
反射的,参考一下:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>



using System;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{

protected void Page_Load(object sender, EventArgs e)
{

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
System.Web.UI.Page page = GetPage(this) as System.Web.UI.Page;
Button btn = page.FindControl("Button1") as Button;
Type t = btn.GetType();
MethodInfo mi = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(btn, new object[] { e });
}

private Control GetPage(Control ctrl)
{
if (ctrl is System.Web.UI.Page)
return ctrl;
else if (ctrl.Parent != null)
return GetPage(ctrl.Parent);
else
return null;
}
}
}



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>




using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Control ctrl = this.LoadControl("~/WebUserControl1.ascx");
WebUserControl1 wc = ctrl as WebUserControl1;
this.form1.Controls.Add(wc);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('Text changed!');</script>");
}
}
}

  • 打赏
  • 举报
回复
[Quote=引用 20 楼 sp1234 的回复:]
很容易。

我打算花上20分钟写个小的帖子,贴在asp.net论坛里。
[/Quote]

OK,我写了一个帖子。地址在:http://topic.csdn.net/u/20080829/16/b71436c3-9464-4f11-9c0a-387541dad5d3.html
雪狐 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ojlovecd 的回复:]
不用反射吧,直接调用不就行了

C# code
protected void button1_Click(object sender, EventArgs e)
{}
protected void tbx1_TextChanged(object sender, EventArgs e)
{
button1_Click(button1,e);
}
[/Quote]
LQknife 2008-08-29
  • 打赏
  • 举报
回复
高手都来了 晓习
lovefootball 2008-08-29
  • 打赏
  • 举报
回复
那你就在添加的时候直接挂事件就完了

UserControll usercontroll = new UserControll();
usercontroll.changed += new textchange(你的方法);
..............


private void 你的方法(string txt)
{
调用button的Click事件
}
ljhkim6 2008-08-29
  • 打赏
  • 举报
回复
to ojlovecd
在我的页面上 <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />控件是动态添加上去的,
  • 打赏
  • 举报
回复
很容易。

我打算花上20分钟写个小的帖子,贴在asp.net论坛里。
我姓区不姓区 2008-08-29
  • 打赏
  • 举报
回复
参考一下吧,希望对你有用:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>



using System;

namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public delegate void tc(object sender, EventArgs e);

public event tc TextChanged;

protected void Page_Load(object sender, EventArgs e)
{

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if (TextChanged != null)
{
TextChanged(sender, e);
}
}
}
}




<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</div>
</form>
</body>
</html>




using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl11.TextChanged += new WebUserControl1.tc(WebUserControl11_TextChanged);
}

void WebUserControl11_TextChanged(object sender, EventArgs e)
{
Button1_Click(Button1, e);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('Text changed!');</script>");
}
}
}

lovefootball 2008-08-29
  • 打赏
  • 举报
回复
在用户控件里面声明委托和事件

public delegate void textchange(string txt);
public event textchange changed;


在你的TextBox_TextChanged事件里面触发这个事件
private void TextBox_TextChanged(XX,XX)
{
if (changed != null)
{ changed(this.TextBox.Text); }//把TextBox的值传过去
}

在你加载你的控件的Page里面去监视这个事件
在属性框里面双击changed事件就可以了
然后写调用Button的代码就OK了~~~~~
lude8880 2008-08-29
  • 打赏
  • 举报
回复
好多狐狸精都修行几千年的
lude8880 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ojlovecd 的回复:]
引用 8 楼 lude8880 的回复:
引用 4 楼 ojlovecd 的回复:
引用 2 楼 lude8880 的回复:
输入参数coder返回一个CEO对象的方法应该怎么写
year=year+200

那样return 的是一个DeadBody对象,不是CEO对象,谢谢

你不信佛?

信佛就可以不用死啊?你信佛?那你200年后来给我扫墓吧
[/Quote]
死了就转世了20年后又是一条好汉你不知道?
我姓区不姓区 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 ljhkim6 的回复:]



那你应该在用户控件加一个事件,这个事件在TextBox_TextChanged时触发,那么在容器页面就可以捕捉到这个事件了
[/Quote]

你说法很对
有代码可以参考吗??
[/Quote]
你稍等,我写写看
ljhkim6 2008-08-29
  • 打赏
  • 举报
回复



那你应该在用户控件加一个事件,这个事件在TextBox_TextChanged时触发,那么在容器页面就可以捕捉到这个事件了
[/Quote]

你说法很对
有代码可以参考吗??
我姓区不姓区 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ljhkim6 的回复:]
想实现的效果是
在用户控件里的TextBox里的值发生改变后,让他执行页面上的Serach_Click()事件.
说百了就是如何在用户控件中的事件里调用页面中的事件.

[/Quote]
那你应该在用户控件加一个事件,这个事件在TextBox_TextChanged时触发,那么在容器页面就可以捕捉到这个事件了
JaggerLee 2008-08-29
  • 打赏
  • 举报
回复
原来是 ascx 调用 当前aspx的BUTTON_CLICK
我姓区不姓区 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lude8880 的回复:]
引用 4 楼 ojlovecd 的回复:
引用 2 楼 lude8880 的回复:
输入参数coder返回一个CEO对象的方法应该怎么写
year=year+200

那样return 的是一个DeadBody对象,不是CEO对象,谢谢

你不信佛?
[/Quote]
信佛就可以不用死啊?你信佛?那你200年后来给我扫墓吧
ljhkim6 2008-08-29
  • 打赏
  • 举报
回复
想实现的效果是
在用户控件里的TextBox里的值发生改变后,让他执行页面上的Serach_Click()事件.
说百了就是如何在用户控件中的事件里调用页面中的事件.
加载更多回复(9)

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧