加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 动态地将用户控件添加到转发器

发布时间:2020-12-16 03:25:30 所属栏目:asp.Net 来源:网络整理
导读:我有一个表示嵌套层次结构的类(MyClass),因此该类有一个属性,它是MyClass的集合. MyClass还有一个title属性 为了在网页上显示它,我希望创建一个具有转发器的用户控件.在项目模板中,我将使用文字来显示title属性,并且在转发器的ItemCreated事件上,我将创建use
我有一个表示嵌套层次结构的类(MyClass),因此该类有一个属性,它是MyClass的集合. MyClass还有一个title属性

为了在网页上显示它,我希望创建一个具有转发器的用户控件.在项目模板中,我将使用文字来显示title属性,并且在转发器的ItemCreated事件上,我将创建usercontrol的新实例,并将其添加到转发器中的当前项目中.

我的问题是,当usercontrol中的Page_Load事件触发时,如果控件是动态添加的,则repMyClass转发器poroperty为null,即使我调用EnsureChildControls.我在这里错过了什么吗?如果我创建一个转发器并将我的userctonrol放入项目模板中,它可以正常工作.我不能让它动态地工作吗?

用户控制:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="MyControl" %>
Items:<br/>
<asp:Repeater ID="repMyClass" runat="server" EnableViewState="false" 
    OnItemCreated="repMenuItems_ItemCreated">
    <HeaderTemplate><ul><HeaderTemplate>
    <ItemTemplate>
        <li><%# Eval("Title") %>
            <div><asp:PlaceHolder ID="SubItemPlaceholder" runat="server" /></div>
        </li></ItemTemplate>
    <FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

用户控制代码:

public partial class MyControl: System.Web.UI.UserControl
{
    public IEnumerable<MyClass> ChildItems { get; set; }
    protected void Page_Load(object sender,EventArgs e)
    {
        this.repMyClass.DataSource = ChildItems;
        this.repMyClass.DataBind();
    }

    protected void repMenuItems_ItemCreated(object Sender,RepeaterItemEventArgs  e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            //Get the MyClass instance for this repeater item
            MyClass mcCurrent = (MyClass)e.Item.DataItem;

            //If the MyClass instance has child instances
            if (mcCurrent.Children != null && mcCurrent.Children.Length > 0)
            {
                //Add a new user control and set it's property so it can bind
                PlaceHolder ph = (PlaceHolder)e.Item.FindControl("SubItemPlaceholder");

                MyControl ctl = (MyControl)Page.LoadControl(typeof(MyControl),new object[] {});

                ctl.ChildItems = mcCurrent.Children;
                ph.Controls.Add(ctl);
            }
        }
    }
}

解决方法

我很久以前就已经使用Accordions创建了嵌套报告.

在Index中,当您要动态添加用户控件实例时:

// Declare Placeholder    
PlaceHolder ph = (PlaceHolder)e.Item.FindControl("SubItemPlaceholder")

// Adding some literal controls to header
ph.Controls.Add(new LiteralControl("This is the accordion header!!"));

// Declare new control variable
crt = new Control();

// Load up your User Control
crt = LoadControl("~/MyControl.ascx");

// Check if it has loaded properly
if (crt != null)
{
    // GET / SET any custom properties of the User Control
    ((myClass)crt).title = "Welcome";

    // Add the new User Control to the placeholder's controls collection
    ph.Controls.Add(crt);
}

注意:
在用户控件中,必须在声明标记中添加“ClassName”

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyControl.ascx.cs" Inherits="myTest" ClassName="myClass" %>

此外,您希望在动态创建实例时公开的任何属性,您可以按如下方式声明它们:

public string title { get; set; }

因此,如果要在创建“repMyClass”时强制设置值,可以将其设置为属性,并以编程方式为其指定任何值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读