c# - 从后面的代码访问用户控件 (ascx) 的父 DIV

标签 c# asp.net code-behind

从 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/

相关文章:

javascript - UpdatePanel 内的 ASP.NET GridView 直到第二次单击才会更新

asp.net - 在代码隐藏文件 C# 中将 boundField 添加到 gridview

c# - Web Api 项目中的 MVC 应用程序 Cookie 和 token 身份验证

c# - 如何在控制标签内包含条件语句?

wpf - 在后面的代码中查找类型的默认样式

asp.net - 跨应用程序的表单例份验证

asp.net - li标签图片定制

c# - System.Collections.Generic.LinkedList`1.AddLast(T 值)中的 NullReferenceException

c# - 找不到类型或命名空间名称 'SelectListItem',MVC3

C#格式化字符串日期