且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

加载的ASP.NET用户控件嵌套

更新时间:2023-12-06 10:11:16

当您添加从背后code用户控件,你必须实际加载控件。你可以做这样的事情做到这一点:

 保护无效的Page_Load(对象发件人,EventArgs的)
{
    的for(int i = 0;我小于5;我++)
    {
        InnerUserControl CUC =(InnerUserControl)this.Page.LoadControl(InnerUserControl.ascx);

        holder.Controls.Add(CUC);
    }
 }
 

假定它们是在同一目录中。如果不是,把虚拟路径作为参数传递给LoadControl方法。你并不需要有寄存器声明的外部控制,当你做到这一点。此外,我通常使用在code前,而不是一个div标签。我不知道这是否有差别,但是。我还建议加载控件的OnInit,因为这将确保生命周期运行的各个阶段为您的用户控件。

祝您好运!

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..
}

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. 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!