How can I get the parent page from a User Control in an ASP.NET Website (not Web Application)

后端 未结 9 1088
别跟我提以往
别跟我提以往 2020-12-15 16:54

Just as the subject asks.

EDIT 1

Maybe it\'s possible sometime while the request is being processed to store a reference to the parent page in the user contr

9条回答
  •  我在风中等你
    2020-12-15 17:07

    Create a delegate in the user control and then assign it a method from the parent page.

    class MyUserControl : UserControl
    {
       delegate object MyDelegate(object arg1, object arg2, object argN);
       public MyDelegate MyPageMethod;
    
       public void InvokeDelegate(object arg1, object arg2, object argN)
       {
         if(MyDelegate != null)
            MyDelegate(arg1, arg2, argN); //Or you can leave it without the check 
                                          //so it can throw an exception at you.
       }
    }
    
    class MyPageUsingControl : Page
    {
       protected void Page_Load(object sender, EventArgs e)
       {
         if(!Page.IsPostBack)
            MyUserContorlInstance.MyPageMethod = PageMethod;
       }
    
       public object PageMethod(object arg1, object arg2, object argN)
       {
         //The actions I want
       }
    }
    

提交回复
热议问题