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); } 注意: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyControl.ascx.cs" Inherits="myTest" ClassName="myClass" %> 此外,您希望在动态创建实例时公开的任何属性,您可以按如下方式声明它们: public string title { get; set; } 因此,如果要在创建“repMyClass”时强制设置值,可以将其设置为属性,并以编程方式为其指定任何值. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何阻止ASP.Net MVC Html.ActionLink使用现
- ASP.NET Core 下自定义权限验证
- asp.net-mvc-3 – mvc3 – 在不同的区域使用部分视图
- 如何在IIS10中正确启动asp.net应用程序
- 来自asp.net中的config xml的quartz.net
- asp.net-mvc-3 – ASP.NET MVC 3和Razor的ASHX处理程序
- 如何在ASP.NET MVC 4和jquery中使用web api下载文件
- asp.net-mvc – Spark View Engine的性能与ASP.NET相比如何
- 解决ASP.NET中的后台任务中的Autofac组件
- 定期刷新局部视图(ASP.Net MVC)
推荐文章
站长推荐
- asp.net – 无法返回JsonResult
- asp.net – System.Web.Security.AntiXss.AntiXs
- asp.net – 在Owin有来自Global.asax的Applicati
- asp.net-mvc – 在BaseController中获取/设置Htt
- asp.net – OData和区分大小写
- asp.net – 如何在OnActionExecuting中获取当前路
- asp.net – 具有w3wp应用程序池IIS 7的高内存使用
- asp.net – 带LinqDataSource的DropDownList和一
- .NET Framework 3.5中有哪些有用的新ASP.NET功能
- asp.net-mvc – 返回空ActionResult
热点阅读