C# C.net中的动态中继器
我正在尝试使用ITemplate动态创建嵌套的中继器。这个中继器就像我传递列表一样,它生成中继器。问题是,当我对外部中继器进行数据绑定时。仅显示最后一个嵌套的中继器。下面是屏幕截图。 加价C# C.net中的动态中继器,c#,.net,asp.net,C#,.net,Asp.net,我正在尝试使用ITemplate动态创建嵌套的中继器。这个中继器就像我传递列表一样,它生成中继器。问题是,当我对外部中继器进行数据绑定时。仅显示最后一个嵌套的中继器。下面是屏幕截图。 加价 您需要为parentRpt.ItemDataBound事件编写处理程序,并设置childRpt的DataSource属性,然后从那里调用child.Rpt.DataBind。为什么不创建生成自定义控件并用代码填充它呢 override CreateChildControls(){ foreac
您需要为parentRpt.ItemDataBound事件编写处理程序,并设置childRpt的DataSource属性,然后从那里调用child.Rpt.DataBind。为什么不创建生成自定义控件并用代码填充它呢
override CreateChildControls(){
foreach(var r in rows)
{
...
}
}
模板、重新创建器等只是为了提高设计的时效性,在您手动构建所有模板的情况下,我看不出为什么会过度复杂地使用占位符、模板等
只需构建CompositeControl并用代码填充它。您将两次减少代码,并且您将拥有可读性最好的代码。避免恶意事件连接子重新创建器数据源属性
对不起,太硬了…好了,伙计们,我知道了。。。。谢谢大家。我犯了个错误。模板类是错误的。我修复了它,现在它工作得很好 下面是课堂
据我所知,对数据绑定的调用是分层的。我的意思是调用parent将导致所有子控件按原样进行数据绑定。这就是正在发生的事情。否则,即使最后一行也不会出现。这可能是一个解决方案。但是,这个问题是整个问题的小部分。我想做的是生成嵌套的rpts frm xml。Tht xml实际上是我们公司定义的一个标准,用于验证中继器内的控件。我正在尝试生成UI来测试这些规则。所以,我不得不使用rpts。这是我第一次讨论堆栈溢出。所以我想我不能再评论了。这就是我使用聊天语言的原因。对不起。
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;
}
}
}
}
override CreateChildControls(){
foreach(var r in rows)
{
...
}
}
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;
}
}
}