需求很简单:控件触发事件时需要调用page的PageFunction函数。现在的思路:声明一个接口实现该方法
public interface ITest
{
     void PageFunction();
}
限制page实现该接口
public partial class WebUserControl<T>: System.Web.UI.UserControl where T:ITest
{
//.............
}请问调用的时候该怎么写?
((T)this.Page.GetType).GridViewBindData();
上面那样可以吗?不用反射是因为反射的效率很低,速度上达不到要求。

解决方案 »

  1.   

    如果某个对象实现了,
    直接调用接口的PageFunction函数
      

  2.   

    public partial class MyUerControl : System.Web.UI.UserControl
    {
        Function fu;
        private string _id;
        public string id
        {
            get
            {
                string tempid = "";
                if (ViewState["id"] != null)
                {
                    tempid = ViewState["id"].ToString();
                }
                return tempid;
            }
            set { ViewState["id"]=_id = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }
        publice void BindDropDowsList()
        {
            this.DropDownList1.DataSorce=BindGroup();
            this.DropDownList1.DataTextField="name";
            this.DropDownList1.DataValueField="key";
            this.DropDownList1.DataBind();
        }
        Dictionary<string, string> BindGroup()
        {
            Dictionary<string, string> group =
               new Dictionary<string, string>();        group.Add("超级管理员", "0");
            group.Add("管理员", "1");
            group.Add("用户", "2");
            group.Add("只读", "3");
            return group;
        }
    }
    --------------------------
    在用到用户控件的地方用  控件.BindDropDowsList()即可
      

  3.   

    支持,还可以绑定gridview 几乎所有的服务器控件都可以绑定泛型 而且不需要加装各种架构
      

  4.   

    6楼的代码我能看懂。
    但是我的需求是控件调page的函数,可否再详细说明一下?
      

  5.   

    System.Web.UI.UserControl where T:ITest 
    都这么继承了,不反射,好像也做不到绑定吧?
    本身继承的就是接口,不反射,也不知道你要实例化那一个类,还有你也没有贴出继承这个接口的类,你要反射什么啊?有点晕。
      

  6.   

    作为控件,应该尽量减少对Page的依赖,以降低耦合度。这种控件主动判断Page类型然后调用的方法,是不好的。