Calling a method in parent page from user control

前端 未结 10 1152
暖寄归人
暖寄归人 2020-11-28 23:01

I\'ve a user control registered in an aspx page On click event of a button in the user control, how do i call a method which is there in the parent page\'s code

相关标签:
10条回答
  • 2020-11-28 23:17

    I love Stephen M. Redd's answer and had to convert it to VB. Sharing it here. Suggested edits welcome.

    In the user control's code-behind:

    Public Class MyUserControl
        Inherits System.Web.UI.UserControl
    
        'expose the event publicly
        Public Event UserControlButtonClicked As EventHandler 
    
        'a method to raise the publicly exposed event
        Private Sub OnUserControlButtonClick()
    
            RaiseEvent UserControlButtonClicked(Me, EventArgs.Empty)
    
        End Sub
    
        Protected Sub lbtnApplyChanges_Click(sender As Object, e As EventArgs) Handles lbtnApplyChanges.Click
    
            'add code to run here, then extend this code by firing off this event
            'so it will trigger the public event handler from the parent page, 
            'and the parent page can hanlde it
    
            OnUserControlButtonClick()
    
        End Sub
    
    End Class
    

    In the parent page subscribe to the event, so when the event is raised, your code will run here.

    Public Class SalesRecord
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            'hook up event handler for exposed user control event to handle this
            AddHandler MyUserControl.UserControlButtonClicked, AddressOf MyUserControl_UserControlButtonClicked
    
        End Sub
    
        ''' <summary>
        ''' code to run when user clicks 'lbtnApplyChanges' on the user control
        ''' </summary>
    
        Private Sub MyUserControl_UserControlButtonClicked()
    
            'this code will now fire when lbtnApplyChanges_Click executes
    
        End Sub
    
    End Class
    
    0 讨论(0)
  • 2020-11-28 23:20

    Here is the classic example using events as suggested by Freddy Rios (C# from a web application project). This assumes that you want to use an existing delegate rather than make your own and you aren't passing anything specific to the parent page by event args.

    In the user control's code-behind (adapt as necessary if not using code-behind or C#):

    public partial class MyUserControl : System.Web.UI.UserControl
    {
        public event EventHandler UserControlButtonClicked;
    
        private void OnUserControlButtonClick()
        {
            if (UserControlButtonClicked != null)
            {
                UserControlButtonClicked(this, EventArgs.Empty);
            }
        }
    
        protected void TheButton_Click(object sender, EventArgs e)
        {
            // .... do stuff then fire off the event
            OnUserControlButtonClick();
        }
    
        // .... other code for the user control beyond this point
    }
    

    In the page itself you subscribe to the event with something like this:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // hook up event handler for exposed user control event
            MyUserControl.UserControlButtonClicked += new  
                        EventHandler(MyUserControl_UserControlButtonClicked);
        }
        private void MyUserControl_UserControlButtonClicked(object sender, EventArgs e)
        {
            // ... do something when event is fired
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 23:22

    Scott Allen has a useful article on event bubbling from a user control to the parent page, which elaborates on the answer provided by Stephen M. Redd:

    • Event Bubbling From Web User Controls in ASP.NET (C#)
    0 讨论(0)
  • 2020-11-28 23:23

    I suggest you don't call the page method directly, as you would be tying your control to the specific page.

    Instead expose an event, and have the page subscribe to it. It works for any number of pages, can more easily be used when the control is multiple times on a single page (perhaps even on a list) and is more in line with asp.control design.

    0 讨论(0)
  • 2020-11-28 23:26

    You can use Session . Say when you click on a button in user control and when you want to call Parent page method then in event handler of button click set value as

    Session["CallParent"]="someValue";
    

    as button will post back the page . On Page_Load event of Parent Page add it

    protected void Page_Load(object sender,EventArgs e)
    {
       if(Session["CallParent"]!=null)
       {
          // here call the Parent Page method 
          Method1();
          // now make the session value null
         Session["CallParent"]=null;
        }
    }
    

    It is much more efficient

    0 讨论(0)
  • 2020-11-28 23:30

    I posted something that works here: Access control > page > master nested > master

    Or if you want access to a control you can also do this:

    Update parent page Where parent has an update panel named "UpdatePanel1"

    Control

    UpdatePanel onParent1 = (UpdatePanel)Parent.FindControl("UpdatePanel1");
    onParent1.Update();
    
    0 讨论(0)
提交回复
热议问题