2009-04-04 8 views
0

Ich versuche, verschachtelte Repeater dynamisch mit ITemplate zu erstellen. Dieser Repeater ist so, dass ich List<Control> übergebe und es Repeater generiert. Problem ist, dass wenn ich einen äußeren Repeater bestimme. Nur der letzte verschachtelte Repeater zeigt. Folgendes ist Screenshot. screen shot http://i40.tinypic.com/axjwpf.jpgDynamischer Repeater in C# .net

Markup


<%@ 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> 

Code hinter



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

    } 
} 

Antwort

0

Ok Jungs habe ich es aus .... Vielen Dank an alle. Ich habe einen Fehler gemacht. Die Template-Klasse wurde wrong.I es behoben und jetzt funktioniert es wie Charm follwoing ist die Klasse

 

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

0

Sie müssen Handler für parentRpt.ItemDataBound-Ereignis schreiben und DataSource-Eigenschaft von childRpt festlegen und dann von dort child.Rpt.DataBind() aufrufen.

+0

Wie pro meines Wissens Aufruf databind hierarchisch ist. Ich meine, der Anruf zum Elternteil wird bewirken, dass alle untergeordneten Steuerelemente so wie es geht. Das ist was passiert. Sonst wird auch die letzte Zeile nicht angezeigt. – Mohit

0

Warum nicht einfach benutzerdefinierte Steuerelemente erstellen und mit Ihrem Code füllen?

override CreateChildControls(){ 

    foreach(var r in rows) 
    { 
    ... 
    } 
} 

Vorlagen, repreaters und sind so auf nur Design expirience besser zu machen, in Ihrer Situation, wenn Sie alle von Hand bauen ich einen Grund nicht sehen kann, warum mit all dem Platzhalter, Vorlagen verkomplizieren und so weiter ...

Erstellen Sie einfach CompositeControl und füllen Sie es mit Ihrem Code. Sie werden Ihren Code zweimal reduzieren und den besten Code haben. Without bösen Ereignisse, um Child Repreater DataSource-Eigenschaften zu verbinden. obwohl

Sorry für harte offtop ...

+0

Es kann eine Lösung sein. Aber diese Frage ist ein kleines Bild des ganzen Problems. Was ich versuche zu tun ist, nested rpts frm xml zu erzeugen. Tht xml ist eigentlich ein Standard, der von unserer Comp definiert wurde, um Kontrollen in Repeatern zu validieren. Ich versuche, UI zu generieren, um diese Regeln zu testen. Also muss ich rpts verwenden. – Mohit

+0

das ist mein erstes Mal auf Stack Overflow.So dachte ich, ich kann nicht noch einmal kommentieren. Deshalb habe ich Chatsprache benutzt. Das tut mir leid. – Mohit