0

I am working with the web application which I have only binaries. The application allows developing extension by providing internals to the derived classes.

What I would like to achieve is to add additional functionality to the existing aspx page. Because I can’t replicate and modify corresponding code I thought about developing a UserControl and modify markup of the discussed aspx page to include that control.

Until now I was successful. The control appears on the page and triggers server event handlers. It also has access to the application internal data. What I need now is to programmatically manipulate some of the original page elements from that UserControl server side.

I know that this is not the purpose of User Controls and they should not to have any knowledge about parrent page elements. However, this is as far as I know the only way to include some of the custom functionality to the existing page.

Before I asked this question I have spent a good amount of time for researching avaiable solutions.

Please, could you suggest the best possible way of referencing those elemnent from the User Control at server side?

Below is a simplified code representation of what I have done so far

The existing aspx page:

// SOME CONTENT OF THE ORGINAL PAGE

<%@ Register TagPrefix="uc" TagName="MyControl" Src="~/usercontrols/MyControl.ascx" %>

// SOME CONTENT OF THE ORGINAL PAGE

<uc:MyControl ID="pnlMyControl" runat="server"></uc:MyControl>

// SOME CONTENT OF THE ORGINAL PAGE

My User Control:

public partial class MyControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //DO SOMETHING HAVING ACCESS TO INTERNAL DATA
    }

    protected void lnkTest_Click(object sender, EventArgs e)
    {
        //DO SOMETHING HAVING ACCESS TO INTERNAL DATA
    }
}
Dimt
  • 2,278
  • 2
  • 20
  • 26
  • Possible duplicate of [Get access to parent control from user control - C#](https://stackoverflow.com/questions/8820606/get-access-to-parent-control-from-user-control-c-sharp) – Michael Freidgeim Aug 30 '19 at 13:43

1 Answers1

0

I've done something similar, though I've done it for variables rather than controls so I shall attempt to adapt it here.

I used properties to achieve this, working with:

<%@ Register TagPrefix="uc" TagName="MyControl" Src="~/usercontrols/MyControl.ascx" %>

<uc:MyControl ID="pnlMyControl" runat="server" />

In the UserControl you do something like:

private string _myTextBox = new TextBox();

public string myTextBox
{
    get 
    {
        return _myTextBox;
    }
    set
    {
        _myTextBox = value;
    }
}

Then in the parent page, you can set these properties; for example:

protected void Page_Load(object sender, EventArgs e)
{
    pnlMyControl.myTextBox = MyParentTextBox;
}

Also, you may find this useful; you can directly fire methods on the parent from the user control using this:

this.Page.GetType().InvokeMember("MyMethodName", System.Reflection.BindingFlags.InvokeMethod, null, this.Page, new object[] { });

I hope that helps.

Hugo Yates
  • 2,081
  • 2
  • 26
  • 24
  • Unfortunatelly I can't edit the parent's page code behind as i only have binary. – Dimt Feb 27 '15 at 16:38
  • Sorry I missed that bit. You could attempt to route to them via `Parent.Controls` and/or `Parent.FindControl()` – Hugo Yates Feb 27 '15 at 17:02