我正在尝试使用ITemplate动态创建嵌套转发器。这个转发器就像我正在通过List<Control>
并生成转发器。问题是当我数据库外转发器。仅显示最后一个嵌套转发器。以下是截屏。
加价
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Restricted_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="border:solid 1px black;">
<tr>
<td>
<asp:PlaceHolder ID="phControls" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
代码背后
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Restricted_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateNestedRepeater();
}
private void CreateNestedRepeater()
{
Repeater childRpt = new Repeater();
List repeatingRuleControls = new List();
repeatingRuleControls.Add(new TextBox());
repeatingRuleControls.Add(new TextBox());
repeatingRuleControls.Add(new TextBox());
RepeatingRuleTemplate repeatingRuleTemplate = new RepeatingRuleTemplate(ListItemType.Item, repeatingRuleControls);
childRpt.HeaderTemplate = new RepeatingRuleTemplate(ListItemType.Header, repeatingRuleControls);
childRpt.ItemTemplate = repeatingRuleTemplate;
childRpt.FooterTemplate = new RepeatingRuleTemplate(ListItemType.Footer, null);
childRpt.DataSource = new DataRow[4];
Repeater parentRpt = new Repeater();
repeatingRuleControls = new List();
repeatingRuleControls.Add(new TextBox());
repeatingRuleControls.Add(new TextBox());
repeatingRuleControls.Add(new TextBox());
repeatingRuleControls.Add(childRpt);
RepeatingRuleTemplate parentrepeatingRuleTemplate = new RepeatingRuleTemplate(ListItemType.Item, repeatingRuleControls);
parentRpt.HeaderTemplate = new RepeatingRuleTemplate(ListItemType.Header, repeatingRuleControls);
parentRpt.ItemTemplate = parentrepeatingRuleTemplate;
parentRpt.FooterTemplate = new RepeatingRuleTemplate(ListItemType.Footer, null);
parentRpt.DataSource = new DataRow[4];
parentRpt.DataBind();
phControls.Controls.Add(parentRpt);
}
public class RepeatingRuleTemplate : ITemplate
{
ListItemType templateType;
List innerControls;
public RepeatingRuleTemplate(ListItemType type, List controls)
{
templateType = type;
innerControls = controls;
}
public void InstantiateIn(Control container)
{
PlaceHolder ph = new PlaceHolder();
switch (templateType)
{
case ListItemType.Header:
ph.Controls.Add(new LiteralControl(""));
ph.Controls.Add(new LiteralControl(""));
foreach (Control control in innerControls)
{
Label label = new Label();
label.Text = control.ID;
ph.Controls.Add(new LiteralControl(""));
ph.Controls.Add(label);
ph.Controls.Add(new LiteralControl(""));
}
ph.Controls.Add(new LiteralControl(""));
break;
case ListItemType.Item:
ph.Controls.Add(new LiteralControl(""));
foreach (Control control in innerControls)
{
if (control.GetType() != typeof(Repeater))
{
ph.Controls.Add(new LiteralControl(""));
TextBox textBox = new TextBox();
textBox.ID = control.ID;
ph.Controls.Add(textBox);
ph.Controls.Add(new LiteralControl(""));
}
else
{
ph.Controls.Add(new LiteralControl(""));
ph.Controls.Add(control as Repeater);
//(control as Repeater).DataSource = new DataRow[4];
// (control as Repeater).DataBind();
ph.Controls.Add(new LiteralControl(""));
}
}
ph.Controls.Add(new LiteralControl(""));
//ph.DataBinding += new EventHandler(Item_DataBinding);
break;
case ListItemType.Footer:
ph.Controls.Add(new LiteralControl(""));
break;
}
container.Controls.Add(ph);
}
public List Controls
{
get
{
return innerControls;
}
}
}
}
答案 0 :(得分:0)
您需要为parentRpt.ItemDataBound事件编写处理程序并设置childRpt的DataSource属性,然后从那里调用child.Rpt.DataBind()。
答案 1 :(得分:0)
为什么不只是制作自定义控件并用代码填充它?
override CreateChildControls(){
foreach(var r in rows)
{
...
}
}
模板,代理等等只是为了让设计更好,在您手动构建所有内容的情况下,我无法看到为什么与所有占位符,模板等过度复杂的原因......
只需构建CompositeControl并用您的代码填充它。您将减少两次代码,并且您将拥有可读代码的最佳代码。 Witout令人讨厌的事件,以连接子代理DataSource属性。
抱歉硬顶...
答案 2 :(得分:0)
好的家伙我想通了......感谢所有人。我犯了一个错误。模板类错了。我修复了它,现在它像Charm一样工作 Follwoing是班级
public class RepeatingRuleTemplate : ITemplate
{
ListItemType templateType;
List innerControls;
public RepeatingRuleTemplate(ListItemType type, List<Control> controls)
{
templateType = type;
innerControls = controls;
}
public void InstantiateIn(Control container)
{
PlaceHolder ph = new PlaceHolder();
switch (templateType)
{
case ListItemType.Header:
ph.Controls.Add(new LiteralControl("<table border=\"0\">"));
ph.Controls.Add(new LiteralControl("<tr>"));
foreach (Control control in innerControls)
{
Label label = new Label();
label.Text = control.ID;
ph.Controls.Add(new LiteralControl("<td>"));
ph.Controls.Add(label);
ph.Controls.Add(new LiteralControl("</td>"));
}
ph.Controls.Add(new LiteralControl("</tr>"));
break;
case ListItemType.Item:
ph.Controls.Add(new LiteralControl("<tr>"));
foreach (Control control in innerControls)
{
//ph.Controls.Add(new LiteralControl("<td>"));
//ph.Controls.Add(control as TextBox);
//ph.Controls.Add(new LiteralControl("</td>"));
if (control.GetType() != typeof(Repeater))
{
ph.Controls.Add(new LiteralControl("<td>"));
TextBox textBox = new TextBox();
textBox.ID = control.ID;
ph.Controls.Add(textBox);
ph.Controls.Add(new LiteralControl("</td>"));
}
else
{
ph.Controls.Add(new LiteralControl("<td>"));
Repeater rpt = new Repeater();
rpt.DataSource = (control as Repeater).DataSource;
rpt.ItemTemplate = (control as Repeater).ItemTemplate;
rpt.HeaderTemplate = (control as Repeater).HeaderTemplate;
rpt.FooterTemplate = (control as Repeater).FooterTemplate;
rpt.DataBind();
ph.Controls.Add(rpt);
//(control as Repeater).DataSource = new DataRow[4];
// (control as Repeater).DataBind();
ph.Controls.Add(new LiteralControl("</td>"));
}
}
ph.Controls.Add(new LiteralControl("</tr>"));
//ph.DataBinding += new EventHandler(Item_DataBinding);
break;
case ListItemType.Footer:
ph.Controls.Add(new LiteralControl("</table>"));
break;
}
container.Controls.Add(ph);
}
public List<Control> Controls
{
get
{
return innerControls;
}
}
}
public RepeatingRuleTemplate(ListItemType type, List<Control> controls)
{
templateType = type;
innerControls = controls;
}
public void InstantiateIn(Control container)
{
PlaceHolder ph = new PlaceHolder();
switch (templateType)
{
case ListItemType.Header:
ph.Controls.Add(new LiteralControl("<table border=\"0\">"));
ph.Controls.Add(new LiteralControl("<tr>"));
foreach (Control control in innerControls)
{
Label label = new Label();
label.Text = control.ID;
ph.Controls.Add(new LiteralControl("<td>"));
ph.Controls.Add(label);
ph.Controls.Add(new LiteralControl("</td>"));
}
ph.Controls.Add(new LiteralControl("</tr>"));
break;
case ListItemType.Item:
ph.Controls.Add(new LiteralControl("<tr>"));
foreach (Control control in innerControls)
{
//ph.Controls.Add(new LiteralControl("<td>"));
//ph.Controls.Add(control as TextBox);
//ph.Controls.Add(new LiteralControl("</td>"));
if (control.GetType() != typeof(Repeater))
{
ph.Controls.Add(new LiteralControl("<td>"));
TextBox textBox = new TextBox();
textBox.ID = control.ID;
ph.Controls.Add(textBox);
ph.Controls.Add(new LiteralControl("</td>"));
}
else
{
ph.Controls.Add(new LiteralControl("<td>"));
Repeater rpt = new Repeater();
rpt.DataSource = (control as Repeater).DataSource;
rpt.ItemTemplate = (control as Repeater).ItemTemplate;
rpt.HeaderTemplate = (control as Repeater).HeaderTemplate;
rpt.FooterTemplate = (control as Repeater).FooterTemplate;
rpt.DataBind();
ph.Controls.Add(rpt);
//(control as Repeater).DataSource = new DataRow[4];
// (control as Repeater).DataBind();
ph.Controls.Add(new LiteralControl("</td>"));
}
}
ph.Controls.Add(new LiteralControl("</tr>"));
//ph.DataBinding += new EventHandler(Item_DataBinding);
break;
case ListItemType.Footer:
ph.Controls.Add(new LiteralControl("</table>"));
break;
}
container.Controls.Add(ph);
}
public List<Control> Controls
{
get
{
return innerControls;
}
}
}