如何使用反射的方式在用户控件的tbx1_TextChanged事件里调用aspx页面里的button事件?
解决方案 »
- Random不随机。。
- c# 平台下有没有好的集群软件
- WinFrom GridView获取某行 第1列的值
- 移入显示漂浮控件
- 点对点语音聊天
- 怎样在一个大文本文件中读我需要的内容呢?
- 一个涉及到接口的问题,高手请入
- ASP.net访问remoting无限等待的问题
- WINFORM里对GRID里的数据修改后。如何让GRID的当前更新行选中
- 在Asp.net 用C#中写一个方法里面用到SqlParameter,要用到一个时间格式的参数,那么这个参数要怎么写,
- 反射,事件
- 学了一年的java,现在转向了.net,刚刚开始学呢,请问各位大虾有没有什么好的建议或者是好的学习方法?另外我想买一本关于.net的书看看?帮忙推荐哈子,先谢谢大家了!
{}
protected void tbx1_TextChanged(object sender, EventArgs e)
{
button1_Click(button1,e);
}
year=year+200
aspx.csprotected void button1_Click(object sender, EventArgs e)
{} usercontrl.ascxprotected void tbx1_TextChanged(object sender, EventArgs e)
{
button1_Click(button1,e);
}
button1_Click这个事件在usercontrl.ascx根本找不到,编译错误不识别
然后加载这个用户控件的Page去监视这个事件
然后在监视事件的地方在调用Button的Click事件~~
你也不知道加载你这个控件的Page有没有Button在控件里面去调用父页面的任何内容,这种设计都是有问题的~~~~
在用户控件里的TextBox里的值发生改变后,让他执行页面上的Serach_Click()事件.
说百了就是如何在用户控件中的事件里调用页面中的事件.
那你应该在用户控件加一个事件,这个事件在TextBox_TextChanged时触发,那么在容器页面就可以捕捉到这个事件了
[/Quote]你说法很对
有代码可以参考吗??
你说法很对
有代码可以参考吗??
[/Quote]
你稍等,我写写看
public event textchange changed;
在你的TextBox_TextChanged事件里面触发这个事件
private void TextBox_TextChanged(XX,XX)
{
if (changed != null)
{ changed(this.TextBox.Text); }//把TextBox的值传过去
}在你加载你的控件的Page里面去监视这个事件
在属性框里面双击changed事件就可以了
然后写调用Button的代码就OK了~~~~~
<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>");
}
}
}
在我的页面上 <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />控件是动态添加上去的,
usercontroll.changed += new textchange(你的方法);
..............
private void 你的方法(string txt)
{
调用button的Click事件
}
OK,我写了一个帖子。地址在:http://topic.csdn.net/u/20080829/16/b71436c3-9464-4f11-9c0a-387541dad5d3.html
<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>");
}
}
}
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