从 ASP .NET Web 用户控件 (ascx
) 的代码隐藏中,我需要(在运行时)访问其父级 div 元素。目的很简单,就是修改所提到的父 div 的可见性。
我无法触及太多的网页代码,因此我需要一个仅需要修改用户控件后面的代码的解决方案。
因此,在网页代码的 HTML“部分”中,我有以下内容:
<div id="parentDIV" runat="server">
<uc:MyUserControl ID="myUserControlInstance" runat="server" />
</div>
我想在用户控件后面的代码中执行如下操作:
this.Container.Visible = false;
请注意,我并不是在问这样做是否是一个好的做法。
编辑: 后面的用户控制代码并不“知道”父 DIV 的 ID。
最佳答案
我会将其隐藏在客户端上。使用“awesomeUserControl”之类的类来装饰您的用户控件容器(div?)。然后使用 ScriptManager 对象发出一些 javascript 来隐藏父级,如下所示:
ScriptManager.RegisterStartupScript(this, this.GetType(), "HideMyAwesomeUserControl", "$('.awesomeUserControl').parent().hide();", true);
关于c# - 从后面的代码访问用户控件 (ascx) 的父 DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11002148/