简体   繁体   English

在ASP.NET中加载嵌套的UserControl

[英]Loading Nested UserControls in ASP.NET

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. 基本上,我有一个Chart的包装控件,该控件可以选择数据,绑定和自定义图表,并具有很多抽象性。 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: 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: 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: 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: 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: 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..
}

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: 您可以通过执行以下操作来做到这一点:

protected void Page_Load(object sender, EventArgs e)
{
    for(int i=0;i<5;i++)
    {
        InnerUserControl cuc = (InnerUserControl)this.Page.LoadControl("InnerUserControl.ascx");

        holder.Controls.Add(cuc);
    }
 }

Assuming they are in the same directory. 假设它们在同一目录中。 If not, put the virtual path as the parameter to the LoadControl method. 如果不是,则将虚拟路径作为LoadControl方法的参数。 You don't need to have the register statement on the outer control when you do this. 执行此操作时,无需在外部控件上具有register语句。 Also, I usually use an on the code front instead of a div tag. 另外,我通常在代码前面使用,而不是div标签。 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. 我还将建议加载控件OnInit,因为这将确保为用户控件运行生命周期的所有阶段。

Good Luck! 祝好运!

Have you tried adding 您是否尝试过添加

<%@ Reference Control="InnerUserControl.ascx" %>

to OuterUserControl.ascx 到OuterUserControl.ascx

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. 与其在后台代码中实例化控件对象并将其添加到控件集合中(我认为您可能需要LoadControl()才能正确执行并且比page_load早做才能将其连接到页面生命周期中),而是添加内部控件在OuterUserControl中声明。

If you need to add the InnerUserControl 5 times (like the example) declare the InnerUseControl inside a repeater. 如果您需要添加InnerUserControl 5次(如示例所示),则在中继器内声明InnerUseControl。 That is bound to the list of id's. 那是绑定到id的列表。

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="OuterUserControl.ascx.cs" Inherits="Proj.OuterUserControl" EnableViewState="false" %>
<%@ Register src="InnerUserControl.ascx" tagname="InnerUserControl" tagprefix="uc1" %>
       <asp:Repeater ID="Repeater1" runat="server" DataSourceID="src">
    <ItemTemplate>
      <uc1:InnerUserControl id="iuc" runat="server" ID='<%#Eval("ID")%>'/>
    </ItemTemplate>
    </asp:Repeater>

    <asp:ObjectDataSource ID="src" runat="server" SelectMethod="IDList" 
        TypeName="Web.Default"></asp:ObjectDataSource>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM