如何使用反射的方式在用户控件的tbx1_TextChanged事件里调用aspx页面里的button事件?

解决方案 »

  1.   

    不用反射吧,直接调用不就行了protected void button1_Click(object sender, EventArgs e)
    {}
    protected void tbx1_TextChanged(object sender, EventArgs e)
    {
        button1_Click(button1,e);
    }
      

  2.   

    输入参数coder返回一个CEO对象的方法应该怎么写
    year=year+200
      

  3.   

    说不定人家不知道button事件叫啥名字。也就是不知道你的button1_Cli
      

  4.   

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

  5.   

     
    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根本找不到,编译错误不识别
      

  6.   

    你应该在用户控件的tbx1_TextChanged事件里面抛一个事件出来
    然后加载这个用户控件的Page去监视这个事件
    然后在监视事件的地方在调用Button的Click事件~~
      

  7.   

    因为你在控件里面根本不知道谁会加载你这个控件
    你也不知道加载你这个控件的Page有没有Button在控件里面去调用父页面的任何内容,这种设计都是有问题的~~~~
      

  8.   

    想实现的效果是
    在用户控件里的TextBox里的值发生改变后,让他执行页面上的Serach_Click()事件.
    说百了就是如何在用户控件中的事件里调用页面中的事件.
      

  9.   

    原来是  ascx 调用  当前aspx的BUTTON_CLICK
      

  10.   

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

  11.   

     
    那你应该在用户控件加一个事件,这个事件在TextBox_TextChanged时触发,那么在容器页面就可以捕捉到这个事件了
    [/Quote]你说法很对
    有代码可以参考吗??
      

  12.   


    你说法很对
    有代码可以参考吗??
    [/Quote]
    你稍等,我写写看
      

  13.   

    在用户控件里面声明委托和事件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了~~~~~
      

  14.   

    参考一下吧,希望对你有用:<%@ 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>");
            }
        }
    }
      

  15.   

    很容易。我打算花上20分钟写个小的帖子,贴在asp.net论坛里。
      

  16.   

    to ojlovecd
    在我的页面上 <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />控件是动态添加上去的,
      

  17.   

    那你就在添加的时候直接挂事件就完了UserControll usercontroll = new UserControll();
    usercontroll.changed += new textchange(你的方法); 
    ..............
    private void 你的方法(string txt)
    {
       调用button的Click事件
    }
      

  18.   


    OK,我写了一个帖子。地址在:http://topic.csdn.net/u/20080829/16/b71436c3-9464-4f11-9c0a-387541dad5d3.html
      

  19.   

    反射的,参考一下:<%@ 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>");
            }
        }
    }
      

  20.   

    最终答案:
    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
      

  21.   

    http://blog.csdn.net/linghuxiaochong/archive/2008/11/11/3275521.aspx