【发布时间】:2012-01-09 23:42:59
【问题描述】:
我的 ASP.Net 应用程序中有一个基本用户控件,此用户控件中有 HTML 标记,设置为 runat="server" 和 id。我遇到的问题是当加载用户控件时,HTML 标记被返回为 null
ASP.Net C# 代码:
public partial class webonlinecustombase : System.Web.UI.UserControl
{
public Event Event { get; set; }
public OnlineSystemPageCustom custompage { get; set; }
public OnlineSystemPageCustom.OnlineSystemPageHdr.OnlineSystemPageModule custommodule { get; set; }
public void Page_Load(object sender, EventArgs e)
{
string typeName = custommodule.ModuleInternetFile;
inpagelink.HRef = "#" + custommodule.ModuleName.Replace(" ", "").Replace("/", "");
modtitle.InnerText = custommodule.ModuleName;
Type child = Type.GetType(typeName);
UserControl ctl = Activator.CreateInstance(child) as UserControl;
if (ctl != null)
{
this.modsection.Controls.Add(ctl);
}
}
}
这是 HTML 标记:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="webonlinecustombase.ascx.cs" Inherits="IPAMIntranet.IPAM_Controls.webtemplatecontrols.webonlinecustombase" %>
<a id="inpagelink" runat="server"></a>
<span id="modtitle" runat="server" style="width:100%;text-align:left">Scientific Overview</span>
<div id="modsection" runat="server" style="width:100%;">
</div>
<p><a href="#top" class="bodylink">Back to Top</a></p>
为什么inpagelink 和modtitle 返回为空?
【问题讨论】:
-
您不应该将 UserControl 作为 Page.LoadControl("...") 作为 UserControl 加载而不是激活它的类型吗?
-
webonlinecustombase 是这样加载的,但是从它继承的控件是完全动态的,所以我不知道要调用哪些控件。我还能将 Page.LoadControl 用于动态控件吗?
标签: c# asp.net dynamic user-controls null