0

I am trying to create a page using Ajax Tabs and user controls. The .aspx page contains a reference to a default control

<%@ Register src="~/Controls/DefaultControl.ascx" tagname="DefaultControl" tagprefix="uc1" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <uc1:DefaultControl ID="DefaultControl1" runat="server" />
    <%--<uc2:CorrespondenceControl ID="CorrespondenceControl" runat="server" />--%>
</asp:Content>

And the DefaultControl.ascx is using Ajax Tabs, one of which contains a child control within an Update Panel

asp:TabPanel ID="tbpnl2" runat="server" HeaderText="Tab With GridView with select buttons" Visible="True">
  <ContentTemplate>
    <asp:UpdatePanel ID="updpnl2" runat="server" UpdateMode="Conditional">
      <ContentTemplate>
        <uc2:Control1 ID="Control1" runat="server" />
      </ContentTemplate>
    </asp:UpdatePanel>
  </ContentTemplate>
</asp:TabPanel>

The DefaultControl holds a method in the code behind page which is successfully called directly from other tabs (with the markup contained directly in DefaultControl.ascx) on the DefaultControl.ascx page to change the display when Select is clicked on a gridview -

public void ShowPage()
{
            gv1.DataBind();
            fv1.DataBind();
            tbpnl1.Visible = true; //show details tab
            tbpnl2.Visible = true;
            tab1.ActiveTabIndex = 1; //set details tab as current tab
            txt.Text = String.Empty;
            updPnl1.Update();
}

I am trying to call this method from the child Control1 when Select on a gridview is selected there, but obviously none of the elements referenced are in Control1.

I have been searching for a way to be able to use the existing method and have seen a number of suggestions including Interfaces, references like ((DefaultControl)this.DefaultControl).ShowPage(); on the code behind Control1

But as I am just starting to program I have no idea how to implement any of these solutions or what the syntax should be to get them to work.

Is there a simple, even if dirty, way to use the method from a parent control in a child control contained in an Ajax tab?

1 Answers1

0

Not sure if this is what you are looking for... Below example shows calling of direct UserControl and nested UserControl method's from Web page


Default.aspx

<%@ Register TagPrefix="uc" TagName="WebUserControl" Src="WebUserControl.ascx" %>
<%@ Register TagPrefix="uc2" TagName="WebUserControl2" Src="WebUserControl2.ascx" %>
<form runat="server" id="form1">
    <uc:WebUserControl ID="control1" runat="server" />
    <hr />
    <h4>
        At Default.aspx</h4>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Call the function" />
</form>  

Default.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
    control1.CallMe();
    var control2 = (WebUserControl2)control1.FindControl("control2");
    control2.CallMe2();
}

WebUserControl.ascx

<%@ Register TagPrefix="uc2" TagName="WebUserControl2" Src="WebUserControl2.ascx" %>
<div runat="server">
    <h3>
        WebUserControl</h3>
    <asp:Label ID="lbl1" Text="I am ready at WebUserControl" runat="server"></asp:Label>
    <div runat="server" id="toAdd" style="color: Red;">
    </div>
</div>
<hr />
<uc2:WebUserControl2 ID="control2" runat="server" />  

WebUserControl.ascx.cs

public void CallMe()
{
    Label lbl = new Label();
    lbl.Text = "I am at WebUserControl";
    toAdd.Controls.Add(lbl);
}

WebUserControl2.ascx

<div runat="server">
    <h3>
        WebUserControl2</h3>
    <asp:Label ID="lbl1" Text="I am ready at WebUserControl2" runat="server"></asp:Label>
    <div runat="server" id="toAdd" style="color: Red;">
    </div>
</div>

WebUserControl2.ascx.cs

public void CallMe2()
{
    Label lbl = new Label();
    lbl.Text = "I am at WebUserControl2";
    toAdd.Controls.Add(lbl);
}  

Hope it helps someone...!!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Bhavik
  • 4,836
  • 3
  • 30
  • 45
  • Thanks for this. I have it up and running and am just dissecting it now. I don't think it is quite what I was looking for as the 'button' I have is a built in Select on a gridview on the equivalent of WebUserControl2. However I may be able to shuffle things about. I think 'code'var control2 = (WebUserControl2)control1.FindControl("control2");'code' could be a very useful if I can use it with a gv Select. – user3595641 May 02 '14 at 15:12
  • @user3595641 you could use `FindControl` to find `select in gv` and then add `selectedindex change EventListner` like `selectvar.SelectedIndexChanged+=new EventHandler(select_SelectedIndexChanged);` – Bhavik May 02 '14 at 15:21