在 ASP.NET 中加载嵌套用户控件
我遇到了嵌套控件无法正确加载的问题。 我尝试了各种页面方法,但似乎没有任何效果。
编辑:该网站编译并运行良好 - 但它只是留下了一个空白屏幕。
基本上,我有一个图表的包装控件,它允许我选择数据、绑定和自定义带有大量抽象的图表。 我需要另一个包装控件,因为我可能想轻松地表示一些图表组。
通用答案也很棒。
这就是我所拥有的。 我已用填充符替换了自定义属性/属性:
Default.aspx:
<%@ Page Language="C#" CodeBehind="Default.aspx.cs" %>
<%@ Register src="OuterUserControl.ascx" tagname="OuterUserControl" tagprefix="uc2" %>
<uc2:OuterUserControl ID="uc1" runat="server" Att1="foo" Att2="bar" />
OuterUserControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="OuterUserControl.ascx.cs" Inherits="Proj.OuterUserControl" EnableViewState="false" %>
<%@ Register src="InnerUserControl.ascx" tagname="InnerUserControl" tagprefix="uc1" %>
<div runat="server" id="holder"></div>
OuterUserControl.ascx.cs:
private member variables
Att1
Att2
protected void Page_Load(object sender, EventArgs e)
{
for(int i=0;i<5;i++)
{
InnerUserControl cuc = new InnerUserControl(id);
holder.Controls.Add(cuc);
}
}
InnerUserControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="InnerUserControl.ascx.cs" Inherits="Proj.InnerUserControl" %>
<asp:Chart ID="chart" runat="server"></asp:Chart>
InnerUserControl.ascx.cs:
private int id;
//private member variables
public InnerUserControl(int id)
{
this.id = id;
this.chart = new Chart();
}
protected void Page_Load(object sender, EventArgs e)
{
//Get data for given id
//Databind chart
//etc..
}
I've got an issue with nested controls not loading properly. I've tried various page methods and nothing seems to work.
EDIT: The site compiles and runs fine--it just leaves a blank screen though.
Basically I have a wrapper control for a Chart that allows me to select data, bind, and customize the chart with a lot of abstraction. I need another wrapper control for that because there may be groups of charts that I want to represent easily.
Generalized answers would be great too.
Here's what I have. I've replaced my custom properties/attributes with fillers:
Default.aspx:
<%@ Page Language="C#" CodeBehind="Default.aspx.cs" %>
<%@ Register src="OuterUserControl.ascx" tagname="OuterUserControl" tagprefix="uc2" %>
<uc2:OuterUserControl ID="uc1" runat="server" Att1="foo" Att2="bar" />
OuterUserControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="OuterUserControl.ascx.cs" Inherits="Proj.OuterUserControl" EnableViewState="false" %>
<%@ Register src="InnerUserControl.ascx" tagname="InnerUserControl" tagprefix="uc1" %>
<div runat="server" id="holder"></div>
OuterUserControl.ascx.cs:
private member variables
Att1
Att2
protected void Page_Load(object sender, EventArgs e)
{
for(int i=0;i<5;i++)
{
InnerUserControl cuc = new InnerUserControl(id);
holder.Controls.Add(cuc);
}
}
InnerUserControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="InnerUserControl.ascx.cs" Inherits="Proj.InnerUserControl" %>
<asp:Chart ID="chart" runat="server"></asp:Chart>
InnerUserControl.ascx.cs:
private int id;
//private member variables
public InnerUserControl(int id)
{
this.id = id;
this.chart = new Chart();
}
protected void Page_Load(object sender, EventArgs e)
{
//Get data for given id
//Databind chart
//etc..
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您从后面的代码添加用户控件时,您必须实际加载该控件。 您可以通过执行以下操作来完成此操作:
假设它们位于同一目录中。 如果没有,则将虚拟路径作为参数传递给 LoadControl 方法。 执行此操作时,不需要在外部控件上使用 register 语句。 另外,我通常在代码前面使用 an 而不是 div 标签。 不过,我不确定这是否会产生影响。 我还建议加载控件 OnInit,因为这将确保用户控件的生命周期的所有阶段都运行。
祝你好运!
When you add a user control from the code behind, you have to actually load the control. You can do this by doing something like this:
Assuming they are in the same directory. If not, put the virtual path as the parameter to the LoadControl method. You don't need to have the register statement on the outer control when you do this. Also, I usually use an on the code front instead of a div tag. I'm not sure if this makes a difference, however. I would also recommend loading the control OnInit, as this will ensure all phases of the Lifecycle run for your user controls.
Good Luck!
您是否尝试添加
到 OuterUserControl.ascx
Have you tried adding
to OuterUserControl.ascx
我建议采用不同的方法。 添加内部控件在 OuterUserControl 中以声明方式进行。
如果您需要添加 InnerUserControl 5 次(如示例),请在转发器内声明 InnerUseControl。 这与 id 列表绑定。
I'd recommend a different approach. Instead of instantiating the control object in the code-behind and adding it to the controls collection (which I think you'd probably need LoadControl() to do properly and do it earlier than page_load to hook it into the page lifecycle), add the inner control declaratively in the OuterUserControl.
If you need to add the InnerUserControl 5 times (like the example) declare the InnerUseControl inside a repeater. That is bound to the list of id's.