locked
How to refresh the main page from a user control RRS feed

  • Question

  • User-1188570427 posted

    I have a user control on my web forms page.

    How can I call a method from my user control code behind to update the page that the user control is on?

    Tuesday, May 29, 2018 9:33 AM

Answers

All replies

  • User475983607 posted

    I have a user control on my web forms page.

    How can I call a method from my user control code behind to update the page that the user control is on?

    Fire an event in the user control and listen for that event in the content page,

    https://stackoverflow.com/questions/7880850/how-do-i-make-an-event-in-the-usercontrol-and-have-it-handled-in-the-main-form?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 29, 2018 9:56 AM
  • User-1188570427 posted

    tvb2727

    I have a user control on my web forms page.

    How can I call a method from my user control code behind to update the page that the user control is on?

    Fire an event in the user control and listen for that event in the content page,

    https://stackoverflow.com/questions/7880850/how-do-i-make-an-event-in-the-usercontrol-and-have-it-handled-in-the-main-form?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

    I tried the below, but the event handler is not coming up as a property to even set:

    You need to create an event handler for the user control that is raised when an event from within the user control is fired. This will allow you to bubble the event up the chain so you can handle the event from the form.
    
    When clicking Button1 on the UserControl, i'll fire Button1_Click which triggers UserControl_ButtonClick on the form:
    
    User control:
    
    public event EventHandler ButtonClick;
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        //bubble the event up to the parent
        if (this.ButtonClick!= null)
            this.ButtonClick(this, e);               
    }
    Form:
    
    UserControl1.ButtonClick += new EventHandler(UserControl_ButtonClick);
    
    protected void UserControl_ButtonClick(object sender, EventArgs e)
    {
        //handle the event 
    }

    Thursday, June 7, 2018 11:24 PM
  • User-1188570427 posted

    mgebhard

    tvb2727

    I have a user control on my web forms page.

    How can I call a method from my user control code behind to update the page that the user control is on?

    Fire an event in the user control and listen for that event in the content page,

    https://stackoverflow.com/questions/7880850/how-do-i-make-an-event-in-the-usercontrol-and-have-it-handled-in-the-main-form?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

    I tried the below, but the event handler is not coming up as a property to even set:

    You need to create an event handler for the user control that is raised when an event from within the user control is fired. This will allow you to bubble the event up the chain so you can handle the event from the form.
    
    When clicking Button1 on the UserControl, i'll fire Button1_Click which triggers UserControl_ButtonClick on the form:
    
    User control:
    
    public event EventHandler ButtonClick;
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        //bubble the event up to the parent
        if (this.ButtonClick!= null)
            this.ButtonClick(this, e);               
    }
    Form:
    
    UserControl1.ButtonClick += new EventHandler(UserControl_ButtonClick);
    
    protected void UserControl_ButtonClick(object sender, EventArgs e)
    {
        //handle the event 
    }

    I found this article that helped me fix the issue. Still not sure why the event wouldn't show up.

    https://stackoverflow.com/questions/623136/calling-a-method-in-parent-page-from-user-control

    Thursday, June 7, 2018 11:33 PM