Search code examples
c#asp.netcode-behind

Access parent DIV of user-control (ascx) from its code behind


From within the code-behind of an ASP .NET web user-control (ascx) I need to get access (at runtime) to its parent, a div element. The aim is simple as to modify the visibility of the mentioned parent div.

I can not touch so much of the web-page code so I'd need a solution requiring only modifications in the user-control's code behind.

So in the HTML "part" of the code of the web page I have this:

<div id="parentDIV" runat="server">
    <uc:MyUserControl ID="myUserControlInstance" runat="server" />
</div>

I'd like to do in the code behind of the user-control something like this:

this.Container.Visible = false;

Note that I'm not asking if it is a good practise or not to do this.

EDIT: The user-control code behind does not "know" about the ID of the parent DIV.


Solution

  • I would hide it on the client. Decorate your user control container (div?) with a class like "awesomeUserControl". Then emit some javascript using the ScriptManager object to hide the parent like this:

    ScriptManager.RegisterStartupScript(this, this.GetType(), "HideMyAwesomeUserControl", "$('.awesomeUserControl').parent().hide();", true);