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

Loading Nested UserControls in ASP.NET(在 ASP.NET 中加载嵌套的用户控件)

本文介绍了在 ASP.NET 中加载嵌套的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了嵌套控件无法正确加载的问题.我尝试了各种页面方法,但似乎没有任何效果.

I've got an issue with nested controls not loading properly. I've tried various page methods and nothing seems to work.

该网站编译并运行良好——但它只是留下一个空白屏幕.

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.

笼统的答案也很好.

这就是我所拥有的.我已经用填充物替换了我的自定义属性/属性:

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);
    }
 }

假设它们在同一个目录中.如果没有,则将虚拟路径作为参数传递给 LoadControl 方法.执行此操作时,您不需要在外部控件上使用 register 语句.另外,我通常在代码前面使用 an 而不是 div 标签.但是,我不确定这是否会有所不同.我还建议加载控件 OnInit,因为这将确保为您的用户控件运行生命周期的所有阶段.

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.

祝你好运!

这篇关于在 ASP.NET 中加载嵌套的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 ASP.NET 中加载嵌套的用户控件

基础教程推荐