Search code examples
c#asp.net

how do you call a method from a user control on button click from buttons outside a view


I have an application where the page has a bunch of views nested in a multiview control and each view has a tab container. inside a specific view there is button that returns the user to the main view. Inside the tabs there are user controls.

my question is how do i call a method inside one of the controls that is in a view with the button that returns the user to the main view?

<asp:MultiView runat="server" ID="MV" ActiveViewIndex="0">
            <asp:View runat="server" ID="view"> 
  <asp:View runat="server" ID="view">
                <asp:LinkButton ID="btnReturn" CssClass="returnBtnLink" Text="Return To Locations" runat="server" OnClick="btnReturn_Click"></asp:LinkButton>
                <cc1:TabContainer ID="tcUser" CssClass="Tab" runat="server" Width="100%" ActiveTabIndex="0" AutoPostBack="true"  Style="width: 100vw; max-width: 58em; display: block; margin-left: auto; margin-right: auto; backface-visibility: hidden; background-color: transparent;">
                <cc1:TabPanel ID="tpUser" runat="server">
                    <HeaderTemplate>
                        <asp:Label ID="Label1" Text="User Info" Width="150px" runat="server"></asp:Label>
                    </HeaderTemplate>
                    <ContentTemplate>
                        <uc1:UserDetails runat="server" ID="ucUserDetails" />
                    </ContentTemplate>
                </cc1:TabPanel>

method i would like to call on btnReturn_Click that is in ucUserDetails:

 protected void setEditOff()
        {}

ii have also put it into a public so it can be seen if needed:

public void resetHistory()
        {
            setEditOff();

        }

Solution

  • In code behind on the main page, you can use this:

     this.ucUserDetails.resetHistory();
    

    Of course the "this" is optional here.

    So, it not 100% clear if you wanting to use general code in the main page, and want to call that code from the main page code behind.

    Of course, there is also the reverse. You want to call some code in the main page code behind from the user control code behind. In this case, then I would add a event method to the user control, and thus just like say you drop in a button on a page, you have a "event" for that button, you can do the same for the user control (have a event for the user control that calls code on the main page).