用户控件.ascx与网页.aspx交互的几种方法

1.aspx文件:
public void SetLabel(string str)
    {
        this.Label1.Text = str; ;
    }
ascx文件;
protected void Button1_Click(object sender, EventArgs e)
    {
        System.Web.UI.Page p = this.Page;
        Type pageType = p.GetType();
        MethodInfo mi = pageType.GetMethod("SetLabel");
        mi.Invoke(p, new object[] { "test!!!" });

    }

2. 在自定义控件中定义这个控件的属性
   public Control LabelText
 {
  get{return this.Label1.Text;}
 }

3用FindControl方法寻找
 Label lbl = (Label)WebUserControl1.FindControl("Label1");

4。委托

(利用事件委托实现用户控件中的行为触发所在页面的处理函数 )

 在日常的工作中,我们时常会遇到这样的需求:点击一用户控件中的服务器按钮,变更页面上该用户控件以外某处的数据显示。这样就需要在发生该按钮点击行为时,触发其所在页面的相应处理函数。在vb.net中,我们可以利用RaiseEvent语句来引发定义在所在页面后台代码文件中的相应处理方法,而在C#中,我们可不可以做到这点呢?

      回答当然是肯定可以实现的。C#中我们可以利用事件委托来达到同样的效果。

      下面,让我们来一步步地解决这个问题。

      1、首先,在一aspx页面中包含一用户控件(RaiseControl.ascx),该用户控件内只含有一服务器端控件。

      2、在该用户控件的后台代码中声明事件委托、定义事件成员并添加一事件监视函数。代码如下:
      

        //声明事件委托
         public delegate void PageChangeEventHandler(string psDeliver);
        //定义事件
         public event PageChangeEventHandler MyPageChange;
        //监视事件
         protected void OnPageChange(string psStr)
         {
             if (MyPageChange != null)
            {
                MyPageChange(psStr);
            }
        }
  
      3、在用户控件的按钮事件中,触发已经定义的MyPageChange事件。
      

        protected void btnRaise_Click(object sender, EventArgs e)
        {
            string _sStr = "你点击了用户控件中的按钮!";
            OnPageChange(_sStr);
        }
          
      4、在其所在的页面中编写相应的处理函数并在页面的Page_Load中将该处理函数绑定到用户控件中定义的MyPageChange事件。
      

         protected void Page_Load(object sender, EventArgs e)
         {
            //利用+=进行事件委托绑定

    //注意不得包含在if(IsPostBack)或者if(!IsPostBack)的里面
              ctlRaiseControl.MyPageChange += this.DealwithReceived;
         }
         
         /**//// <summary>
         /// 为用户控件中MyPageChange事件定义的处理函数
          /// </summary>
        /// <param name="psReceive"></param>
        private void DealwithReceived(string psReceive)
        {
            lblReceive.Text = psReceive;
        }

  机制分析:C#中,委托是一种引用类型。对于一声明委托,我们可以将任意与之具有相同返回类型和参数列表的函数绑定到其上,也就是使该委托指向绑定的具体方法,这一点类似于C++中的函数指针。在以上的示例中,我们同样是将页面后台定义的DealwithReceived方法绑定到了用户控件中声明的PageChangeEventHandler委托上,只不过在委托与具体方法函数之间又隔了一层,这就是“事件”。
      .net事件机制是建立在委托基础之上的。可以说,事件是对委托的封装,这主要是基于面向对象的封装机制。在这里,无论是对委托的绑定,还是对委托指向方法的调用,都是通过event来实现的。
5。aspx页面给ascx页面的控件动态绑定方法

 aspx页面:

 protected void Page_Load(object sender, EventArgs e)
  { 

     //注意不得包含在if(IsPostBack)或者if(!IsPostBack)的里面

                DropDownList ddlMerchant = (EggMerchants1.FindControl("ddlMerchant") as DropDownList);
                ddlMerchant.AutoPostBack = true;
                ddlMerchant.SelectedIndexChanged += new EventHandler(BindEgg);

}

 protected void BindEgg(object sender, EventArgs e)
        {
            ddlEggs.Items.Clear();
            ddlEggs.Items.Add(new ListItem("--请选择蛋--", "-1"));
            List<EggInfo> info = new EggService().QueryEggByMerID(-1, EggMerchants1.MerchantID);
            if (info != null)
            {
                foreach (EggInfo item in info)
                {
                    ddlEggs.Items.Add(new ListItem(string.Format("{0}({1}-{2})", item.EggName, item.BeginDate.Date, item.EndDate.Date), item.RowID.ToString()));
                }
            }
        }

原文地址:https://www.cnblogs.com/noviceliu/p/2116956.html