locked
Call event from a separate control RRS feed

  • Question

  • User1909955381 posted

    I have a page (default.aspx) that contains a control (nav.ascx).  I have an UpdatePanel on my page that needs to be updated when a certain event occurs in my control.

    How can I call an event to update the UpdatePanel on my page from an event in my control?

    Saturday, May 23, 2009 4:53 PM

Answers

  • User6402966 posted

    There are a few ways to do this.

    Option 1: Access the UpdatePanel directly.

    For this example I have a Button (Button1) in my UserControl, and when clicked it will cause the UpdatePanel to update.  The UpdatePanel is accessed directly using the Parent property of the UserControl, which will be the Page.  The problem with this approach is that it tightly binds this UserControl the one page, and if you wanted to use it on multiple pages you'll run into isssues (e.g. the UpdatePanels may have different IDs).

    protected void Button1_Click(object sender, EventArgs e)
    {
    	UpdatePanel UpdatePanel1 = (UpdatePanel)this.Parent.FindControl("UpdatePanel1");
    	// Make any other required changes in the UpdatePanel
    	UpdatePanel1.Update();
    }
    
     

    Option 2: Use an existing event

    If you want the UpdatePanel to update when an existing event occurs in teh UserControl, you can create an event for the UserControl that maps to that event.  In this case I have a Button on the UserControl (Button3) and when it is clicked we want the UpdatePanel to update.

    So, in the UserControl we create an event that maps to the Button Click event:

      

    public event EventHandler Button3Clicked
    {
    	add
    	{
    		Button3.Click += new EventHandler(value);
    	}
    	remove
    	{
    		Button3.Click -= new EventHandler(value);
    	}
    }
    
     

    And on the page we create a handler for that event that updates the UpdatePanel:

    <uc:UserControlEvent ID="UserControlEvent1" runat="server" 
    	OnButton3Clicked="UserControlEvent1_Button3Clicked" />
    
     

      

    protected void UserControlEvent1_Button3Clicked(object sender, EventArgs e)
    {
    	Label1.Text = DateTime.Now.ToString();
    	UpdatePanel1.Update();
    }
    
     

    Option 3: Create a new event

    If there isn't an exisitng event to map to, you can create a unique event for the UserControl.  In this case I'm going to have that event fire when a Button (Button2) is clicked, but that's just an example (really  better fit for Options 2, but it was an easy example).

    So, you create a new event in the UserControl: 

    public event EventHandler Button2Clicked;
    protected virtual void OnButton2Clicked(EventArgs e)
    {
    	if (Button2Clicked != null)
    		Button2Clicked(this, e);
    }
    
     

    Then we have to fire that event.  In this case we'll fire it in the Button2_Click handler, but you could add any required object to trigger the event:

      

    protected void Button2_Click(object sender, EventArgs e)
    {
    	OnButton2Clicked(EventArgs.Empty);
    }
    
     

    Then we add an event handler in the page for this new event:

    <uc:UserControlEvent ID="UserControlEvent1" runat="server" 
    	OnButton2Clicked="UserControlEvent1_Button2Clicked" />
    

      

    protected void UserControlEvent1_Button2Clicked(object sender, EventArgs e)
    {
    	Label1.Text = DateTime.Now.ToString();
    	UpdatePanel1.Update();
    }
    

    Hope that helps.

    Aaron

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 23, 2009 7:14 PM